Steps to create first hibernate application example

Here, we will make the first Hibernate application without IDE. For making the first Hibernate application, we have to need the following steps:

  1. Create the Persistent class
  2. Create the mapping record for Persistent class
  3. Create the Configuration file
  4. Create the class that recovers or stores the steady object
  5. Load the jar file
  6. Run the first Hibernate application by utilizing order prompt

1) Create the Persistent class

A straightforward Persistent class should to need some rules:

  • A no-arg constructor: It is recommended that you have a default constructor in any event bundle perceivability so that Hibernate can make the instance of the Persistent class by newInstance() method.
  • Provide an identifier property: It is smarter to allot a trait as id. This characteristic carries on as an essential key in database.
  • Declare getter and setter methods: The Hibernate perceives the strategy by getter and setter technique names by default.
  • Prefer non-last class: Hibernate utilizes the idea of intermediaries, that relies upon the constant class. The application software engineer won't almost certainly use intermediaries for lethargic affiliation fetching.

Let's make the basic Persistent class:

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.   
  26.   
  27. }  

2) Create the mapping file for Persistent class

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

  • hibernate-mapping : It is the root element in the mapping record that contains all the mapping elements.
  • class : It is the sub-element of the Hibernate mapping element. It determines the Persistent class.
  • id : It is the subelement of class. It indicates the essential key characteristic in the class.
  • generator : It is the sub-element of id. It is utilized to produce the essential key. There are numerous generator classes, for example, appointed, increase, hilo, arrangement, local and so forth. We will get familiar with all the generator classes later.
  • property : It is the sub-element of class that determines 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 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>  

3) Create the Configuration file

The configuration file contains information 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 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.password">jtp</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>  

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.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.     
  12. public class StoreData {    
  13. public static void main(String[] args) {    
  14.         
  15.     //Create typesafe ServiceRegistry object    
  16.     StandardServiceRegistry ssr = new StandardServiceRegistryBuilder().configure("hibernate.cfg.xml").build();  
  17.           
  18.    Metadata meta = new MetadataSources(ssr).getMetadataBuilder().build();  
  19.   
  20. SessionFactory factory = meta.getSessionFactoryBuilder().build();  
  21. Session session = factory.openSession();  
  22. Transaction t = session.beginTransaction();   
  23.             
  24.     Employee e1=new Employee();    
  25.     e1.setId(101);    
  26.     e1.setFirstName("Gaurav");    
  27.     e1.setLastName("Chawla");    
  28.         
  29.     session.save(e1);  
  30.     t.commit();  
  31.     System.out.println("successfully saved");    
  32.     factory.close();  
  33.     session.close();    
  34.         
  35. }    
  36. }   

5) Load the jar file

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

6) How to run the first hibernate application without IDE

We may run this hibernate application by IDE (e.g. Eclipse, Myeclipse, Netbeans etc.) or without IDE. We will learn about creating hibernate application in Eclipse IDE in next chapter.

To run the hibernate application without IDE:

  • Install the oracle10g for this example.
  • Load the jar files for hibernate. (One of the way to load the jar file is copy all the jar files under the JRE/lib/ext folder). It is better to put these jar files inside the public and private JRE both.
  • Now, run the StoreData class by java com.javacodegeeks.mypackage.StoreData
steps to create first hibernate application





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