Hibernate with JPA Annotation Tutorial

The hibernate application can be made with explanation. There are numerous explanations that can be utilized to make hibernate application, for example, @Entity, @Id, @Table and so forth. hibernate Annotations depend on the JPA 2 determination and supports every one of the highlights. All the JPA explanations are characterized in the javax.persistence bundle. hibernate EntityManager actualizes the interfaces and life cycle characterized by the JPA determination. The center favorable position of utilizing hibernate comment is that you don't have to make mapping (hbm) record. Here, hibernate explanations are utilized to give the meta information.

Example to make the hibernate application with Annotation

Here, we will make an expert based hibernate application utilizing comment in shroud IDE. For making the hibernate application in Eclipse IDE, we have to pursue the beneath steps:

1) Create the Maven Project

  • To create the maven project left click on File Menu - New- Maven Project.
Hibernate Application using annotation
  • The new maven project opens in your eclipse. Click Next.
  • Hibernate Application using annotation Now, specify the name of Group Id and Artifact Id. The Group Id contains package name (e.g. com.javacodegeeks) and Artifact Id contains project name (e.g. HibernateAnnotation). Then click Finish. Hibernate Application using annotation

    2) Add project information and configuration in pom.xml file.

    Open pom.xml file and click source. Now, add the below dependencies between <dependencies>....</dependencies> tag. These dependencies are used to add the jar files in Maven project.

    1. <dependency>  
    2.     <groupId>org.hibernate</groupId>  
    3.     <artifactId>hibernate-core</artifactId>  
    4.     <version>5.3.1.Final</version>  
    5. </dependency>  
    6.       
    7. <dependency>  
    8.     <groupId>com.oracle</groupId>  
    9.     <artifactId>ojdbc14</artifactId>  
    10.     <version>10.2.0.4.0</version>  
    11. </dependency>  

    Due to certain license issues, Oracle drivers are not present in public Maven repository. We can install it manually. To install Oracle driver into your local Maven repository, follow the following steps:

    • Install Maven
    • Run the command : install-file -Dfile=Path/to/your/ojdbc14.jar -DgroupId=com.oracle -DartifactId=ojdbc14 -Dversion=12.1.0 -Dpackaging=jar
    • 3) Create the Persistence class.

      Here, we are creating the same persistent class which we have created in the previous topic. But here, we are using annotation.

      @Entity annotation marks this class as an entity.

      @Table annotation specifies the table name where data of this entity is to be persisted. If you don't use @Table annotation, hibernate will use the class name as the table name by default.

      @Id annotation marks the identifier for this entity.

      @Column annotation specifies the details of the column for this property or field. If @Column annotation is not specified, property name will be used as the column name by default.

      To create the Persistence class, right click on src/main/java - New - Class - specify the class name with package - finish.

      Employee.java

      1. package com.javacodegeeks.mypackage;  
      2.   
      3. import javax.persistence.Entity;  
      4. import javax.persistence.Id;  
      5. import javax.persistence.Table;  
      6.   
      7. @Entity  
      8. @Table(name= "emp500")   
      9. public class Employee {    
      10.   
      11. @Id   
      12. private int id;    
      13. private String firstName,lastName;    
      14.     
      15. public int getId() {    
      16.     return id;    
      17. }    
      18. public void setId(int id) {    
      19.     this.id = id;    
      20. }    
      21. public String getFirstName() {    
      22.     return firstName;    
      23. }    
      24. public void setFirstName(String firstName) {    
      25.     this.firstName = firstName;    
      26. }    
      27. public String getLastName() {    
      28.     return lastName;    
      29. }    
      30. public void setLastName(String lastName) {    
      31.     this.lastName = lastName;    
      32. }    
      33. }   

      4) Create the Configuration file

      To create the configuration file, right click on src/main/java - new - file - specify the file name (e.g. hibernate.cfg.xml) - Finish.

      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://www.hibernate.org/dtd/hibernate-configuration-5.3.dtd">  
      5. <hibernate-configuration>  
      6.     <session-factory>  
      7.             
      8.     <property name="hbm2ddl.auto">update</property>    
      9.         <property name="dialect">org.hibernate.dialect.Oracle9Dialect</property>    
      10.         <property name="connection.url">jdbc:oracle:thin:@localhost:1521:xe</property>    
      11.         <property name="connection.username">system</property>    
      12.         <property name="connection.password">jtp</property>    
      13.         <property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property>   
      14.        
      15.         <mapping class="com.javacodegeeks.mypackage.Employee"/>  
      16.     </session-factory>  
      17. </hibernate-configuration>  

      5) Create the class that retrieves or stores the persistent object.

      StoreData.java

      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.     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(101);    
      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. }    
      34. }   

      6) Run the application

      Before running the application, determine that the directory structure is like this.






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