Hibernate : SessionFactory

In this section we will discuss concept of SessionFactory.

Hibernate : SessionFactory

Hibernate : SessionFactory

In this section we will discuss concept of SessionFactory.

Hibernate SessionFactory :

For SessionFactory import org.hibernate.SessionFactory API.

SessionFactory is one of Hibernate concept. It creates session instances. In general an application has a single SessionFactory.
It is immutable and when it is created its internal state is set. This internal state contains metadata about O/R mapping.
The behavior of a sessionFactory is controlled by properties supplied at configuration time.

SessionFactory is thread safe so that many threads can access SessionFactory and request for sessions.

Example : Here we are going to give you example and describing how sessionFactory plays its role and how session is created.

Take a view on Hibernate.cfg.xml - Here we are configuring sessionFactory.

<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"



<!-- Database connection settings -->
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost:3306/hibernate_examples</property>
<property name="connection.username">root</property>
<property name="connection.password">root</property>
<!-- JDBC connection pool (use the built-in) -->
<property name="connection.pool_size">1</property>
<!-- SQL dialect -->
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<!-- Enable Hibernate's automatic session context management -->
<property name="current_session_context_class">thread</property>
<!-- Disable the second-level cache -->
<property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
<!-- Echo all executed SQL to stdout -->
<property name="show_sql">true</property>
<!-- Drop and re-create the database schema on startup -->
<property name="hbm2ddl.auto">none</property>
<mapping class="net.roseindia.table.Employee" />


Main Class HibernateSessionFactory.java -Here we show how sessionFactory creates Session.

package net.roseindia.main;

import java.util.*;
import net.roseindia.util.HibernateUtil;
import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;

public class HibernateSessionFactory {
public static void main(String[] args) {
SessionFactory sessionFactory = HibernateUtil.getSessionFactory(); //Creating instance of SessionFactory
Session session = sessionFactory.openSession(); // session is opened

String hql = "SELECT emp.name, emp.salary, emp.dateOfJoin from Employee emp";
Query query = session.createQuery(hql);

List objectList = query.list();
Iterator iterator = objectList.iterator();
System.out.println("Employee Name \t Salary \t Date of Join");
while (iterator.hasNext()) {
Map map = (Map) iterator.next();
System.out.print("\t\t" + map.get("1"));
System.out.print("\t\t" + map.get("2"));

session.close(); // session is closed

Output :

Hibernate: select employee0_.name as col_0_0_, employee0_.salary as col_1_0_, employee0_.date_of_join as col_2_0_ from employee employee0_
Employee Name     Salary       Date of Join
Mandy             20000        2010-03-12 00:00:00.0
Som               20000        1999-06-22 00:00:00.0
Mac               15000        2003-12-23 00:00:00.0
Roxi              22000        2001-02-03 00:00:00.0
Rowdy             20000        2012-06-27 00:00:00.0
Linda             20000        2010-06-12 00:00:00.0
Lizza             25000        2010-06-12 00:00:00.0

Click here to download complete source code