Hibernate Example using XML in Eclipse

Here, we will make a basic case of Hibernate application utilizing shroud IDE. For making the first rest application in Eclipse IDE, we have to follow the accompanying steps:

  1. Create the java project
  2. Add jar(container) documents for hibernate
  3. Create the persistent class
  4. Create the mapping document for persistent class
  5. Create the Configuration file
  6. Create the class that recovers or stores the persistent object
  7. Run the application

1) Create the java project

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


2) Add jar files for hibernate

To add the jar files Right click on your project - Build path - Add external archives. Now select all the jar files as shown in the image given following then click open.

example to create hibernate application in Eclipse IDE

3) 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.javacodegeeks.mypackage) - finish .

Employee.java

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

4) 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) - ok. It must be outside the package.

employee.hbm.xml

  1. <?xml version='1.0' encoding='UTF-8'?>  
  2. <!DOCTYPE hibernate-mapping PUBLIC  
  3.  "-//Hibernate/Hibernate Mapping DTD 5.3//EN"  
  4.  "http://hibernate.sourceforge.net/hibernate-mapping-5.3.dtd">  
  5.   
  6.  <hibernate-mapping>  
  7.   <class name="com.javacodegeeks.mypackage.Employee" table="emp1000">  
  8.     <id name="id">  
  9.      <generator class="assigned"></generator>  
  10.     </id>  
  11.             
  12.     <property name="firstName"></property>  
  13.     <property name="lastName"></property>  
  14.             
  15.   </class>  
  16.             
  17.  </hibernate-mapping>  

5) Create the Configuration file

The configuration file contains all the informations for the database such as connection_url, driver_class, username, pass etc. The hbm2ddl.auto property is used to create the table in the database automatically. We will have in-depth learning about Dialect class in next chapter. To create the configuration file, right click on src -> new -> file. Now specify the configuration file name e.g. hibernate.cfg.xml.

hibernate.cfg.xml

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

6) Create the class that retrieves or stores the persistent object

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

  1. package com.javacodegeeks.mypackage;  
  2.   
  3. import org.hibernate.Session;  
  4. import org.hibernate.SessionFactory;  
  5. import org.hibernate.Transaction;  
  6. import org.hibernate.boot.Metadata;  
  7. import org.hibernate.boot.MetadataSources;  
  8. import org.hibernate.boot.registry.StandardServiceRegistry;  
  9. import org.hibernate.boot.registry.StandardServiceRegistryBuilder;  
  10.   
  11. public class StoreData {  
  12.   
  13.     public static void main( String[] args )  
  14.     {  
  15.          StandardServiceRegistry ssr = new StandardServiceRegistryBuilder().configure("hibernate.cfg.xml").build();  
  16.             Metadata meta = new MetadataSources(ssr).getMetadataBuilder().build();  
  17.           
  18.         SessionFactory factory = meta.getSessionFactoryBuilder().build();  
  19.         Session session = factory.openSession();  
  20.         Transaction t = session.beginTransaction();  
  21.           
  22.          Employee e1=new Employee();    
  23.             e1.setId(1);    
  24.             e1.setFirstName("Gaurav");    
  25.             e1.setLastName("Chawla");    
  26.          
  27.        session.save(e1);  
  28.        t.commit();  
  29.        System.out.println("successfully saved");    
  30.         factory.close();  
  31.         session.close();     
  32.     }  
  33. }  

7) Run the application

Using compliler you can run java.

example to create first hibernate application in eclipse IDE





javacodegeeks is optimized for learning.© javacodegeeks .
All Right Reserved and you agree to have read and accepted our term and condition