Hibernate 4 SessionFactory Example - Java @ Desk

Saturday, June 13, 2015

Hibernate 4 SessionFactory Example

Hibernate 4 SessionFactory Example

In this post, we will learn how to create org.hibernate.SessionFactory object in Hibernate 4.

Hibernate Version : hibernate-release-4.3.10.Final
JPA Version : JPA 2.1

The hibernate configuration file remains the same. The difference comes while creating the SessionFactory object as the old methods are deprecated in the Hibernate 4.3.10 Final version.


Step 1: Create the Model Annotated Class as shown below
package com.pojo;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name = "Person")
public class Person implements java.io.Serializable {

 private Integer id;
 private String name;

 @Id
 @GeneratedValue
 @Column(name = "ID", unique = true, nullable = false)
 public Integer getId() {
  return this.id;
 }

 public void setId(Integer id) {
  this.id = id;
 }

 @Column(name = "NAME", nullable = false)
 public String getName() {
  return this.name;
 }

 public void setName(String name) {
  this.name = name;
 }
}


Step 2: Creating hibernate.cfg file
<!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
        "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>

 <session-factory>
  <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
  <property name="hibernate.connection.url">jdbc:mysql://localhost:1000/hibernate4</property>
  <property name="hibernate.connection.username">hibernate4</property>
  <property name="hibernate.connection.password">hibernate4</property>
  <property name="show_sql">true</property>
  <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
  <mapping class="com.pojo.Person" />

 </session-factory>
</hibernate-configuration>




Step 3: Create the HibernateUtil class to create SessionFactory object

In Hibernate 4.3.0 and above versions following classes are deprecated:
1) ServiceRegistryBuilder is deprecated
2) Method buildServiceRegistry() is undefined for the type ServiceRegistryBuilder
3) Method buildSessionFactory() from the type Configuration is deprecated

package com.pojo;

import org.hibernate.SessionFactory;
import org.hibernate.boot.registry.StandardServiceRegistry;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;

public class Hibernate4Util {
 private static SessionFactory sessionFactory;

 public static SessionFactory getSessionFactory() {
  if (sessionFactory == null) {
   Configuration cfg = new Configuration()
     .configure("hibernate.cfg.xml");
   StandardServiceRegistryBuilder sb = new StandardServiceRegistryBuilder();
   sb.applySettings(cfg.getProperties());
   StandardServiceRegistry standardServiceRegistry = sb.build();
   sessionFactory = cfg.buildSessionFactory(standardServiceRegistry);
  }
  return sessionFactory;
 }
}


Step 4: Hibernate4 Client Test example
package com.pojo;

import java.util.Iterator;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;

public class Hibernate4Test {
 public static void main(String[] args) throws Exception {

  SessionFactory sessFact = Hibernate4Util.getSessionFactory();
  Session session = sessFact.getCurrentSession();
  org.hibernate.Transaction tr = session.beginTransaction();

  String strSql = "from Person person";
  Query query = session.createQuery(strSql);
  List lst = query.list();
  for (Iterator it = lst.iterator(); it.hasNext();) {
   Person person = (Person) it.next();
   System.out.println("Hello: " + person.getName());
  }
  tr.commit();
  sessFact.close();
 }
}








No comments:

Post a Comment