Example to create the Hibernate Application in MyEclipse

Here, we are going to create a simple example of hibernate application using myeclipse IDE. For creating the first hibernate application in MyEclipse IDE, we need to follow following steps:

  • Create the java project
  • Add hibernate capabilities
  • Create the Persistent class
  • Create the mapping file for Persistent class
  • Add mapping of hbm file in configuration file
  • Create the class that retrieves or stores the persistent object
  • Add jar file for oracle
  • Run the application

Create the java project Create the java project by File Menu - New - project - java project. Presently specify the project name e.g. firsthibernate then next - finish.

Add hibernate capabilities

To add the jar files select your project - click on MyEclipse menu - Project Capabilities - add Hibernate capabilities-next-next Here, Check the Enable dynamic table creation check box to create automatic table - next - Uncheck the create SessionFactory class because we are going to write the code to get session object self for better understanding - finish. Presently configuration file will be created automatically.

Create the Persistent class Here, we are creating the same persistent class which we have created in the previous topic. To create the persistent class, Right click on src - New - Class - specify the class with package name (e.g. com.javatportal.mypackage) - finish.

Employee.java

package com.javatportal.mypackage;

public class Employee {
private int id;
private String firstName,lastName;

public int getId() {
	return id;
}
public void setId(int id) {
	this.id = id;
}
public String getFirstName() {
	return firstName;
}
public void setFirstName(String firstName) {
	this.firstName = firstName;
}
public String getLastName() {
	return lastName;
}
public void setLastName(String lastName) {
	this.lastName = lastName;
}


}

Create the mapping file for Persistent class

Here, we are creating the same mapping file as created in the previous topic. To create the mapping file, Right click on src - new - file - specify the file name (e.g. employee.hbm.xml) - alright. It must be outside the package. Duplicate the dtd for this mapping file from this example after downloading it.

employee.hbm.xml

	
	<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-mapping PUBLIC
 "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
 "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

 <hibernate-mapping>
  <class name="com.javatportal.mypackage.Employee" table="emp1000">
    <id name="id">
     <generator class="assigned"></generator>
    </id>
          
    <property name="firstName"></property>
    <property name="lastName"></property>
          
  </class>
          
 </hibernate-mapping>
 

Add mapping of hbm file in configuration file

open the hibernate.cgf.xml file, and add an entry of mapping resource like this:

 
 <mapping resource="employee.hbm.xml"/>
 

Now the configuration file will look like this:

hibernate.cfg.xml

 
 <?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
          "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
          "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

<hibernate-configuration>

    <session-factory>
        <property name="hbm2ddl.auto">update</property>
        <property name="dialect">org.hibernate.dialect.Oracle9Dialect</property>
        <property name="connection.url">jdbc:oracle:thin:@localhost:1521:xe</property>
        <property name="connection.username">system</property>
        <property name="connection.password">oracle</property>
        <property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
    <mapping resource="employee.hbm.xml"/>
    </session-factory>

</hibernate-configuration>
 

Create the class that retrieves or stores the persistent object

In this class, we are simply storing the employee object to the database.


package com.javatportal.mypackage;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;

public class StoreData {
public static void main(String[] args) {
	
	//creating configuration object
	Configuration cfg=new Configuration();
	cfg.configure("hibernate.cfg.xml");//populates the data of the configuration file
	
	//creating seession factory object
	SessionFactory factory=cfg.buildSessionFactory();
	
	//creating session object
	Session session=factory.openSession();
	
	//creating transaction object
	Transaction t=session.beginTransaction();
		
	Employee e1=new Employee();
	e1.setId(115);
	e1.setFirstName("mithilesh");
	e1.setLastName("kushwaha");
	
	session.persist(e1);//persisting the object
	
	t.commit();//transaction is commited
	session.close();
	
	System.out.println("successfully saved");
	
}
}

Add the jar file for oracle

To add the ojdbc14.jar file, right click on your project - manufacture path - add external archives - select the ojdbc14.jar file - open.