Hibernate with Annotation Example

Hibernate with Annotation Example

 

 

Annotations are very helpful when creating java applications and the same is applicable to hibernate applications. Few example of annotations which can be used while creating hibernate application such as @Table, @Entity, @Id etc.

Annotations in hibernate are based on java persistence api which is also known as JPA 2 specification.

 

Javax.persistence is the package where all annotations are defined.

The main advantage of using hibernate annotation is, you don’t need to create hbm.xml files for mappings.

 

Let’s create the example using hibernate annotation. Here we will be using Eclipse ide and with maven tool.

 

  1. See the below screen shot, create the maven project

Hibernate with annotation example

 

  1. Click next and select catalog type. See below

Annotation Example

 

  1. Click Next from the above steps and once you are one new screen provide Group id and Artifact Id.

Group id contains your package name for example com.clickatme and HibernateAnnotation

 

  1. Now give your project information and configuration in pom.xml
  2. Now create your persistence class and make sure you have the annotations mapped with class attributes.
  3. Annotation @Entity ensures this class as an entity.
  4. Annotation @Table ensures 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.
  5. Annotation @Id ensures the identifier for this entity.
  6. Annotation @Column 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.
  7. To create the Persistence class, right click on src/main/java – New – Class – specify the class name with package – finish.

Employee.java

 

  1. Create your hibernate.cfg.xml file.

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>

 

  1. Create your data store class for Example.

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 DataStore {
  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. }

 

 

Once you complete all the above steps now you are ready to run the applications. Right Click on your DataStore Class and click on run as java application .