Hibernate SessionFactory

This section describes you about the SessionFactory in Hibernate.

Ads

Tutorials   
Hibernate envers custom revinfo table Hibernate 5 Envers Hibernate Envers get all revisions Hibernate Examples Hibernate 5 build SessionFactory Example Hibernate 5 SessionFactory Example Hibernate 5 Annotation Example How to download Hibernate 5.x? Hibernate 5 Features Hibernate 5 Tutorials Learning Hibernate ORM - Free Training/Tutorial videos of Hibernate ORM with Java How to write hello world example program in Hibernate? How to create one to one relationship in Hibernate 4 with Annotation? How to calculate minimum in Hibernate using the min() Function Hibernate ORM 4.2.21.Final Released - Create CURD application in latest Hibernate Hibernate ORM 4.2.21 Tutorial - Learn Hibernate ORM Framework Hibernate Error: Failed to create sessionFactory object.java.lang.NoClassDefFoundError How to create Hibernate Project in Eclipse? Examples of Hibernate Criteria Query Hibernate ORM 4.3.6.Final released Hibernate ORM 4.2.14.Final released Hibernate 4.3.4 Released: Hibernate 4.3.4.Final Maven dependency Hibernate association and join example Learn Hibernate programming with Examples Introduction to Hibernate Framework Hibernate Tutorial: Learn Hibernate with examples Hibernate 4.3 Hello World: Example Hibernate ORM 4.3.1.Final released How to create SessionFactory in Hibernate 4.3.1? Hibernate 4 Hello World: Example of Hello World program Hibernate 4 create Session Factory: Example of creating Session Factory in Hibernate 4 Hibernate 4.3 and JPA 2.1 Example What is Hibernate in Java? Hibernate Overview and Architecture What is JPA 2.1? Hibernate 4.3.0.Final Maven dependency Hibernate 4.3 ORM Features Hibernate 4.3 Tutorial Hibernate Criteria average example Hibernate Criteria load all objects from table Hibernate Criteria Hibernate ORM 4.3.0.Final released Hibernate beginner tutorial Hibernate Session.delete() Example Hibernate save or update method example Hibernate Load Example Hibernate save Example - Rose India Hibernate 4 tutorials Hibernate Example Step by Step in Eclipse Hibernate Configuration files Setup Hibernate Environment
Ads

Hibernate SessionFactory

In this tutorial we will learn about how the SessionFactory creates the session.

SessionFactory is an interface that extends the Referenceable and Serializable interfaces and produces the Session instances. In an application the SessionFactory instance is globally created and is thread safe that the Session instance can be obtained from this factory on receiving request from the client. Once the SessionFactory is created it can't be change, its internal state becomes set. Internal state of SessionFactory contains all of the metadata about ORM.

Syntax :

public interface SessionFactory
extends Referenceable, Serializable

This interface also declares methods. Some of the useful methods are as follows :

  • Session openSession() : This method is used to open Session.

    Syntax : Session openSession() throws HibernateException

    Note : This method is overloaded in this interface with different signatures.
     
  • StatelessSession openStatelessSession() : This method is used to open a stateless Session.

    Syntax : StatelessSession openStatelessSession()
     
  • StatelessSession openStatelessSession(Connection connection) : This method is used to open a stateless Session using the given JDBC Connection.

    Syntax : StatelessSession openStatelessSession(Connection connection)
     
  • boolean isClosed() : This method is used to check whether the SessionFactory is closed or not.

    Syntax : boolean isClosed()
     
  • Session getCurrentSession() : This method is used to get the current Session.

    Syntax : Session getCurrentSession() throws HibernateException
     

Example

Here I am giving a simple example which will demonstrate you about how to use the SessionFactory in the application. This example will explain you how you can open a Session using SessionFactory. In this example we will use the Eclipse IDE for writing and compiling the application and MySQL for database connectivity. Here I have created a Java project in Eclipse then I have created different packages inside the src folder and then created classes inside the packages. I have created the hibernate XML mapping file inside the src folder where I have mapped the Class and its property with the table and its field. Then I have created a hibernate configuration file where I have provide the RDBMS informations inside the <property> sub tag of <session-factory> tag.

Person.java

package net.roseindia;

public class Person
{
int id;
String name;
public Person()
{ 
}
public Person(int id, String name) {
super();
this.id = id;
this.name = name;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}

PersonDetail.java

package net.roseindia;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.service.ServiceRegistryBuilder;

public class PersonDetail
{
private static SessionFactory sessionFactory;
private static ServiceRegistry serviceRegistry;

public static void main(String[] args)
{
Session session = null;
try 
{
try
{
Configuration cfg = new Configuration().addResource(
"person.hbm.xml").configure();
serviceRegistry = new ServiceRegistryBuilder().applySettings(
cfg.getProperties()).buildServiceRegistry();
sessionFactory = cfg.buildSessionFactory(serviceRegistry);
}
catch (Throwable ex)
{
System.err.println("Failed to create sessionFactory object."+ ex);
throw new ExceptionInInitializerError(ex);
}
session = sessionFactory.openSession();
Transaction tx = session.beginTransaction();

Person person = (Person)session.get(Person.class, new Integer(1));
System.out.println("person.getName()=======>"+person.getName());
person.setName("Roseindia");

tx.commit();
session.flush();
System.out.println("Done");
} catch (Exception e) {
System.out.println(e.getMessage());
} finally {
session.close();
}
}
}

Person.hbm.xml

<?xml version='1.0'?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">

<hibernate-mapping package="net.roseindia">
<class name="Person" table="person1">
<id name="id" type="int" column="Id" >
<generator class="assigned"/>
</id>

<property name="name">
<column name="Name" />
</property>

</class>
</hibernate-mapping>

hibernate.cfg.xml

<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD//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:3306/record
</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">root</property>
<property name="hibernate.connection.pool_size">10</property>
<property name="show_sql">true</property>
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.current_session_context_class">thread</property>

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

Download Source Code

Advertisements

Ads
Share on Google+Share on Google+

Hibernate SessionFactory

Posted on: July 11, 2013 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Advertisements

 

Discuss: Hibernate SessionFactory  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:0

Ads

 

Ads