Hibernate example in eclipse
Hibernate First Example
In this section, you will learn how to set environment in eclipse for hibernte application. Follows the following steps:
Step 1: Open eclipse and select the file menu then open the following options. You choose:
Step 2: Click on Java Project then open the following:
Step 3: Click on "Finish" command button. Then your project is created in package explorer. Select your project and follows the following figure:
Step 4: Choose Folder opetion.
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 6: Configure the build path:
Step 7: Click on Add Jar command button then open a dialog box and select all jars:
Step 8: Click on Ok command button:
Step 9: Again click on Ok button. Create a hibernate.cfg.xml file:
<?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">
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">
com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">
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>
</class>
</hibernate-mapping>
|
Step 11: Create a class:
Step 12: Create required fields:
public classEmployee {
private intempId;
private StringempName;
private intempSal;
}
|
Step 13: To create auto generator through the eclipse:
Step 14:Open the following dialog:
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;
}
}
|
Download: Employee.java
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:
Then open the following dialog:
Click on Ok command button:
Output:
Comments
Post a Comment