Hibernate with eclipse Ide

                                              Hibernate Example with Eclipse IDE

 

Here we will be creating the hibernate application with eclipse IDE. For creating it, we need to follow following steps:

  1. Download the eclipse Ide and Create the Java project.
  2.  Add the required jar file
  3. Create the Persistent class
  4. Create the mapping file for Persistent class
  5. Create the Configuration file
  6. Create the class that retrieves or stores the persistent object
  7. Run the first hibernate application with Eclipse IDE

 

  • Download the eclipse ide and create the Java project. For download reference see below screenshot

 

download eclipse ide

 

or click here to download eclipse

 

Once you download the eclipse ide go to menu file and create new project and accordingly give the name of your java project. Example hirsthibernateexample.

 

At the same time please add required jar files for hibernate to the project in lib folder. Perform this right click on project and click on build path and then add external jar files and finish it.

 Hibernate with eclipse ide

 

  •  Create Your Persistent class

A simple Persistent class should follow some rules:

  • A no-arg constructor: It is recommended that you have a default constructor at least package visibility so that hibernate can create the instance of the Persistent class by newInstance() method.
  • Provide an identifier property (optional): It is mapped to the primary key column of the database.
  • Declare getter and setter methods (optional): The Hibernate recognizes the method by getter and setter method names by default.
  • Prefer non-final class: Hibernate uses the concept of proxies that depends on the persistent class. The application programmer will not be able to use proxies for lazy association fetching.

Let’s create the simple Persistent class:

Employee.java

 

  1. package com.clickatme.mypackage;
  2. public class Employee {
  3. private int id;
  4. private String firstName,lastName;
  5. public int getId() {
  6.     return id;
  7. }
  8. public void setId(int id) {
  9.     this.id = id;
  10. }
  11. public String getFirstName() {
  12.     return firstName;
  13. }
  14. public void setFirstName(String firstName) {
  15.     this.firstName = firstName;
  16. }
  17. public String getLastName() {
  18.     return lastName;
  19. }
  20. public void setLastName(String lastName) {
  21.     this.lastName = lastName;
  22. }
  23. }

2) Create the mapping file for Persistent class

The mapping file name conventionally, should be class_name.hbm.xml. There are many elements of the mapping file.

  • hibernate-mapping is the root element in the mapping file.
  • class It is the sub-element of the hibernate-mapping element. It specifies the Persistent class.
  • id It is the subelement of class. It specifies the primary key attribute in the class.
  • generator It is the subelement of id. It is used to generate the primary key. There are many generator classes such as assigned (It is used if id is specified by the user), increment, hilo, sequence, native etc. We will learn all the generator classes later.
  • property It is the subelement of class that specifies the property name of the Persistent class.

Let’s see the mapping file for the Employee class:

employee.hbm.xml

 

  1. <?xml version=’1.0′ encoding=’UTF-8′?>
  2. <!DOCTYPE hibernate-mapping PUBLIC
  3.  “-//Hibernate/Hibernate Mapping DTD 3.0//EN”
  4.  “http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd”>
  5.  <hibernate-mapping>
  6.   <class name=”com.clickatme.mypackage.Employee” table=”emp1000″>
  7.     <id name=”id”>
  8.      <generator class=”assigned”></generator>
  9.     </id>
  10.     <property name=”firstName”></property>
  11.     <property name=”lastName”></property>
  12.   </class>
  13.  </hibernate-mapping>

3) Create the Configuration file

The configuration file contains informations about the database and mapping file. Conventionally, its name should be hibernate.cfg.xml .

hibernate.cfg.xml

 

  1. <?xml version=’1.0′ encoding=’UTF-8′?>
  2. <!DOCTYPE hibernate-configuration PUBLIC
  3.           “-//Hibernate/Hibernate Configuration DTD 3.0//EN”
  4.           “http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd”>
  5. <hibernate-configuration>
  6.     <session-factory>
  7.         <property name=”hbm2ddl.auto”>update</property>
  8.         <property name=”dialect”>org.hibernate.dialect.Oracle9Dialect</property>
  9.         <property name=”connection.url”>jdbc:oracle:thin:@localhost:1521:xe</property>
  10.         <property name=”connection.username”>system</property>
  11.         <property name=”connection.password”>oracle</property>
  12.         <property name=”connection.driver_class”>oracle.jdbc.driver.OracleDriver</property>
  13.     <mapping resource=”employee.hbm.xml”/>
  14.     </session-factory>
  15. </hibernate-configuration>

4) Create the class that retrieves or stores the object

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

 

  1. package com.clickatme.mypackage;
  2. import org.hibernate.Session;
  3. import org.hibernate.SessionFactory;
  4. import org.hibernate.Transaction;
  5. import org.hibernate.cfg.Configuration;
  6. public class StoreData {
  7. public static void main(String[] args) {
  8.     //creating configuration object
  9.     Configuration cfg=new Configuration();
  10.     cfg.configure(“hibernate.cfg.xml”);//populates the data of the configuration file
  11.     //creating seession factory object
  12.     SessionFactory factory=cfg.buildSessionFactory();
  13.     //creating session object
  14.     Session session=factory.openSession();
  15.     //creating transaction object
  16.     Transaction t=session.beginTransaction();
  17.     Employee e1=new Employee();
  18.     e1.setId(115);
  19.     e1.setFirstName(“sonoo”);
  20.     e1.setLastName(“jaiswal”);
  21.     session.persist(e1);//persisting the object
  22.     t.commit();//transaction is commited
  23.     session.close();
  24.     System.out.println(“successfully saved”);
  25. }
  26. }

5) Load the jar file

For successfully running the hibernate application, you should have the hibernate4.jar file.

Some other jar files or packages are required such as

  • cglib
  • log4j
  • commons
  • SLF4J
  • dom4j
  • xalan
  • xerces

 

 

How to run the first hibernate application with Eclipse IDE.

 

Right Click on your store data class and click on run as java application .