Monday, October 12, 2015

Hibernate mapping with Annotation


This is a simple example of mapping a java domain class with a relation using JPA annotation and Hibernate.

Hibernate can be mapped in 2 ways.
  • With an XML mapping file(.hbm file)
  • With Annotation
Description
   We use JPA as Annotation specification and Hibernate as the provider.

Steps

  • Create a java project using Maven in Eclipse and update the pom file

Refer: Hibernate environment setup with maven using Eclipse


  • Create the domain class and annotate using JPA annotation
@Entity - Denotes that instances of this class are persistent with a DB
@Table - Denotes that name of the corresponding table in DB is STUDENT if not used this annotation, table name would be the same as class name(Hence, here it would be Student)
@Id - This field will be the Primary Key column
@Column - Denotes that this will be a column in the table

Mapping can be done for member variables or/and their corresponding getter methods.

package com.sajith.domain;

import javax.annotation.Generated;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;

/**
 * Student Domain class
 *
 */

@Entity
@Table(name="STUDENT")
public class Student {

 
 private int studentId;
 
 private String studentName;
 
 private String studnetCity;

 @Id
 @Column(name="ID")
 public int getStudentId() {
  return studentId;
 }

 
 public void setStudentId(int studentId) {
  this.studentId = studentId;
 }

 @Column(name="CITY")
 public String getStudnetCity() {
  return studnetCity;
 }

 public void setStudnetCity(String studnetCity) {
  this.studnetCity = studnetCity;
 }

 @Column(name="NAME")
 public String getStudentName() {
  return studentName;
 }

 public void setStudentName(String studentName) {
  this.studentName = studentName;
 }

}


  • Create the Hibernate configuration file
  When you use annotation instead of XML mapping only change will be instead of <mapping resource>
<mapping class> attribute.

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
  "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
  "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
    <session-factory>
        <property name="hibernate.connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
        <property name="hibernate.connection.password">sajith</property>
        <property name="hibernate.connection.url">jdbc:oracle:thin:@127.0.0.1:1521:xe</property>
        <property name="hibernate.connection.username">sajith</property>
        <property name="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</property>
        <property name="show_sql">true</property>
        <property name="hbmdl.auto">update</property>
        <mapping class="com.sajith.domain.Student"/>
    </session-factory>
</hibernate-configuration>


  • Create the Util class

package com.sajith.util;

import org.hibernate.SessionFactory;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;

public class HibernateUtil {

 private static final SessionFactory sessionFactory = buildSessionFactory();
 
 private static ServiceRegistry serviceRegistry;

 private static SessionFactory buildSessionFactory() {
  
   Configuration configuration = new Configuration();
   configuration.configure();
   serviceRegistry = new StandardServiceRegistryBuilder().applySettings(
          configuration.getProperties()).build();
  
  return configuration.buildSessionFactory(serviceRegistry);
       
  
 }

 public static SessionFactory getSessionFactory() {
  return sessionFactory;
 }

 public static void shutdown() {
  // Close caches and connection pools
  getSessionFactory().close();
 }
 
}

  • Create the main App class
 
package com.sajith;

import org.hibernate.Session; 
import com.sajith.domain.Student;
import com.sajith.util.HibernateUtil;

public class App {

 
 public static void main(String[] args) {
  
 
  Session session = HibernateUtil.getSessionFactory().openSession();

  session.beginTransaction();
   
  Student student1 = new Student();
  
  student1.setStudentId(1);
                student1.setStudentName("Sanya Fernando");
                student1.setStudnetCity("Kandy");
 
  session.save(student1);
  session.getTransaction().commit();
  
  
 }
 
}
 
  • Run the App file





No comments: