Hibernate in Eclipse
Hibernate in Eclipse
Home Java(Core) Java(Advance) Framework DataBase Tools Servers IDEs PHP Interview Questions Home Framework Hibernate
Tutorials
Struts 2 Struts 1 jQuery Hibernate Hibernate JPA Spring Java Server Faces (JSF) JDBC Servlet JSP Java Ajax Dojo JavaScript HTML XML PHP Core PHP Advance MySQL Interview Questions
Advertise With Us
Previous
Home
Next
Step 3: Click on "Finish" command button. Then your project is created in package explorer. Select your project and follows the following figure:
Step 5: Then lib folder is created under the project. Copy all hibernate library and paste in your lib folder then it will display:
Step 7: Click on Add Jar command button then open a dialog box and select all jars:
<?xml version='1.0' encoding='utf-8'?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD//EN" "http://hibernate.sourceforge.net/ hibernate-configuration-3.0.dtd">
jdbc:mysql://localhost/hibernateExamples</property> <property name="hibernate.connection.username"> root</property> <property name="hibernate.connection.password"> </property> <property name="hibernate.connection.pool_size"> 10</property> <property name="show_sql">true</property> <property name="dialect"> org.hibernate.dialect.MySQLDialect</property> <property name="hibernate.hbm2ddl.auto"> update</property> <!-- Mapping files --> <mapping resource="employee.hbm.xml"/> </session-factory> </hibernate-configuration>
Step 10: Create employee.hbm.xml file:
<?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/ hibernate-mapping-3.0.dtd"> <hibernate-mapping> <class name="developerhelpway.hibernate.Employee" table="employee"> <id name="empId" type="int" column="emp_id" > <generator class="increment"/> </id> <property name="empName"> <column name="emp_name" /> </property> <property name="empSal"> <column name="emp_sal" /> </property>
public classEmployee { private intempId; private StringempName; private intempSal; } Step 13: To create auto generator through the eclipse:
Step 15: Then you get auto generate setter and getter methods:
packagedeveloperhelpway.hibernate; public class Employee { private int empId; private String empName; private int empSal; public int getEmpId() { return empId; }
public void setEmpId(intempId) { this.empId = empId; } public String getEmpName() { return empName; } public voidsetEmpName(String empName) { this.empName = empName; } public int getEmpSal() { return empSal; } public void setEmpSal(intempSal) { this.empSal = empSal; } } Step 16: Now, you create main class: FirstExample.java package developerhelpway.hibernate; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.Transaction; import org.hibernate.cfg.Configuration; public class FirstExample { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub Session sess = null; Transaction tran = null; try{ SessionFactory sessFact = new Configuration().configure().buildSessionFactory(); sess = sessFact.openSession(); System.out.println("Session: "+ sess); tran = sess.beginTransaction();
Employee emp = new Employee(); emp.setEmpName("Birendra Kumar"); emp.setEmpSal(12000); sess.save(emp); tran.commit(); } catch(Exception ex){ ex.printStackTrace(); } finally{ sess.close(); } }
}
To run this application follows the following instructions: Go and select the following:
Download Example
Previous
E-mail Account
Create Your Account Login:
Home
Next
Password:
Remember me
LOGIN
Password forgotten?