Online Training Spring Hibernate Training in Delhi R Programming Training Course Big Data and Hadoop Training Java Training

Hibernate : SessionFactory

In this section we will discuss concept of SessionFactory.


When should you use Hibernate? What's new in Hibernate 5? How much time it takes to learn Hibernate? 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 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 : 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="">none</property>
<mapping class="net.roseindia.table.Employee" />


Main Class -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.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);
System.out.print("\t\t" + map.get("1"));
System.out.print("\t\t" + map.get("2"));

session.close(); // session is closed

Output :

Hibernate: select 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


Share on Google+Share on Google+

Hibernate : SessionFactory

Posted on: August 17, 2012 If you enjoyed this post then why not add us on Google+? Add us to your Circles



Discuss: Hibernate : SessionFactory  

Post your Comment

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