Annotation Example in Hibernate

Hibernate Annotation Example

Hibernate is an ORM (Object-Relational Mapping) tool which simplifies the application development. Hibernate provides a framework which interacts with the data stored in the databases, and it also uses the specifications of the Java Persistence API (JPA).

Hibernate provides annotations for the ease of web development. Without using hibernate-mapping file, we can map a java class to the corresponding table with the help of JPA Annotations. Some of the JPA Annotations are listed below:

  • @Entity- It is used for making a bean class as an entity bean, so it must contain a no-arg constructor.
  • @Table- It is used to create a table for the annotated entity.
  • @Id- The primary key in the table is denoted as @Id.
  • @Generatedvalue- It is used to auto-generate the value of the @Id field.
  • @Column- It is used to define the details of the column to which an attribute is mapped.
  1. Add Dependencies

When using hibernate annotations, you need to add dependencies in pom.xml. Add dependencies between <dependencies>...</dependecies> tag. Some dependencies are listed below:

<dependency>
     <groupId>org.hibernate</groupId>
     <artifactId>hibernate-core</artifactId>
     <version>5.3.1.Final</version> 
 </dependency>
 <dependency>
     <groupId>org.hibernate</groupId> 
     <artifactId>hibernate-validator</artifactId>
     <version>6.0.17.Final</version>
 </dependency> 

You need to add Oracle dependency manually. If you are using Mysql, then you need to add MySQL jar or dependency.

2. Create a POJO/ Bean/ Persistence class

The encapsulation of many objects into a single object is done by POJO class or Bean class.

All the variables of bean class are set as private with public setter and getter methods.

Here we use Annotations in the bean class.

Bean class-Book.java

import javax.persistence.Column;
 import javax.persistence.Entity; 
 import javax.persistence.GeneratedValue; 
 import javax.persistence.GenerationType; 
 import javax.persistence.Id; 
 import javax.persistence.Table; 
 @Entity
 @Table(name="Book01")
 public class Book {
             @Id 
             @GeneratedValue(strategy=GenerationType.AUTO)
             private int bno;
             @Column(name="bname")
             private String bname; 
             @Column(name="price")
             private int price;
             public int getBno() {
                         return bno; 
             }
             public void setBno(int bno) {
                         this.bno = bno;
             } 
             public String getBname() {
                         return bname;
             }
             public void setBname(String bname) { 
                         this.bname = bname;
             }
             public int getPrice() {
                         return price; 
             }
    public void setPrice(int price) {
                         this.price = price;
             }
    } 

3. Create a Configuration File

Let’s create a configuration file that contains information about the mapping file and a relational database.

hibernate.cfg.xml

<?xml version="1.0" encoding="UTF-8"?>  
 <!DOCTYPE hibernate-configuration PUBLIC  
         "-//Hibernate/Hibernate Configuration DTD 5.3//EN"  
         "http://www.hibernate.org/dtd/hibernate-configuration-5.3.dtd">  
 <hibernate-configuration>  
     <session-factory>  
     <property name="hibernate.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.driver_class">oracle.jdbc.driver.OracleDriver</property>   
         <property name="connection.username">root</property>                        //write your username
         <property name="connection.password">root</property>                       //write your password
         <mapping class="com.hibernate.pack.Book"/>  
     </session-factory>  
 </hibernate-configuration>  

4. Create the Application Class

    It is a class which contains the main() methodused to run the application.

    Let’s create AppTest.java.

import org.hibernate.Session; 
import org.hibernate.SessionFactory;                                                                               
import org.hibernate.Transaction;  
public class AppTest{ 
public static void main(String[] args) {    
Configuration cfg= new Configuration();
cfg.configure("hibernate.cfg.xml"); 
SessionFactory factory= cfg.buildSessionFactory();
Session session = factory.openSession();  
Transaction t = session.beginTransaction();   
 Book book=new Book(); 
 book.setBno(401);
 book.setBname("Java Complete Reference");
 book.setPrice(550);
 session.save(book);  
 t.commit();   
  System.out.println("saved successfully ");    
  session.close();    
    }    
   }   

OUTPUT

Annotation Example in Hibernate

Table- book01

Hibernate annonation



ADVERTISEMENT
ADVERTISEMENT