Monday, October 12, 2015

Hibernate environment setup with maven using Eclipse

Hibernate is an ORM tool framework which can be used to abstract the JDBC and access various DBMSs independently from DBMS specific SQL. Hibernate provides a way to developer to write SQL using domain model instead of data model representing in DB.

Hibernate mapping types

1. XML mapping files
2. Annotation

The application of Hibernate


1. Pure Hibernate - Only Hibernate framework is used as the mapping framework.
2. With JPA  - Java standardized API for mapping. Here JPA works as the specification and Hibernate is the provider. This is the recommended development method; while JPA annotation is used as the ORM interface, Hibernate provides the implementation. The advantage is you can easily replace the ORM implementation for another ORM framework.

1. Simple Domain object to Oracle table mapping in Hibernate


Hibernate setup with maven in Eclipse

Required Tools & technologies:
  1. Maven 3.1.1
  2. JBOSS hibernate tools
  3. JDK 1.7
  4. Hibernate 4.3.6.final
  5. Oracle 11g
  6. maven-compiler-plugin 3.3
  7. Ojdbc 11.1.0.7
Steps

  • Create a simple maven project skipping archetype
  • Update .pom file with adding following entries
    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
     <modelVersion>4.0.0</modelVersion>
     <groupId>com.sajith.hibernate</groupId>
     <artifactId>HibernateStart</artifactId>
     <version>0.0.1-SNAPSHOT</version>
    
     <repositories>
      <repository>
       <id>JBoss repository</id>
       <url>http://repository.jboss.org/nexus/content/groups/public/</url>
      </repository>
     </repositories>
            <dependencies>
      <dependency>
       <groupId>org.hibernate</groupId>
       <artifactId>hibernate-core</artifactId>
       <version>4.3.6.Final</version>
      </dependency>
      <dependency>
       <groupId>com.oracle</groupId>
       <artifactId>ojdbc6</artifactId>
       <version>11.1.0.7</version>
      </dependency>
      <dependency>
       <groupId>org.javassist</groupId>
       <artifactId>javassist</artifactId>
       <version>3.18.1-GA</version>
      </dependency>
     </dependencies>
     <build>
      <plugins>
       <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.3</version>
        <configuration>
         <source>1.7</source>
         <target>1.7</target>
        </configuration>
       </plugin>
      </plugins>
     </build>
    </project>
         Here we use a plug-in to change the default maven compiler and run time version from 1.5 to 1.7
         We add JBOSS repository to get the required Hibernate jars downloaded. And, finally add Oracle
         driver. Oracle drive, you have to download manually and install in your local maven repository.

   
  • Create the domain class

package com.sajith.domain;

/**
 * Student Domain class
 *
 */
public class Student {

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

 public int getStudentId() {
  return studentId;
 }

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

 public String getStudnetCity() {
  return studnetCity;
 }

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

 public String getStudentName() {
  return studentName;
 }

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

}
  
  •  Create Hibernate mapping file - Student.hbm.xml
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated Oct 12, 2015 2:15:25 PM by Hibernate Tools 3.4.0.CR1 -->
<hibernate-mapping>
    <class name="com.sajith.domain.Student" table="STUDENT">
        <id name="studentId" type="int">
            <column name="ID" />
            <generator class="assigned" />
        </id>
        <property name="studentName" type="java.lang.String">
            <column name="NAME" />
        </property>
        <property name="studnetCity" type="java.lang.String">
            <column name="CITY" />
        </property>
    </class>
</hibernate-mapping>

   
  • Create Hibernate configuration file - hibernate.cfg.xml
<?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 resource="com/sajith/domain/Student.hbm.xml"/>
    </session-factory>
</hibernate-configuration>

 
Here, xe is the SID
  •  Create Hibernate 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();
  
  
 }
 
}
   
Note that both configuration files should be in class path to run the application.

  • Run the app file and see the result in the DB. you can also see the log result in Eclipse console window
             Hibernate: insert into STUDENT (NAME, CITY, ID) values (?, ?, ?)






No comments: