• Menu
  • Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

JavaBeat

Java Tutorial Blog

  • Java
    • Java 7
    • Java 8
    • Java EE
    • Servlets
  • Spring Framework
    • Spring Tutorials
    • Spring 4 Tutorials
    • Spring Boot
  • JSF Tutorials
  • Most Popular
    • Binary Search Tree Traversal
    • Spring Batch Tutorial
    • AngularJS + Spring MVC
    • Spring Data JPA Tutorial
    • Packaging and Deploying Node.js
  • About Us
    • Join Us (JBC)
  • Java
    • Java 7
    • Java 8
    • Java EE
    • Servlets
  • Spring Framework
    • Spring Tutorials
    • Spring 4 Tutorials
    • Spring Boot
  • JSF Tutorials
  • Most Popular
    • Binary Search Tree Traversal
    • Spring Batch Tutorial
    • AngularJS + Spring MVC
    • Spring Data JPA Tutorial
    • Packaging and Deploying Node.js
  • About Us
    • Join Us (JBC)

openSession and getCurrentSession Difference in Hibernate

December 24, 2013 //  by Krishna Srinivasan//  Leave a Comment

When you get a session from the session factory object in hibernate, either you can use openSession or getCurrentSession. If you are using the openSession method, it opens a new session freshly. If you use getCurrentSession, it gets the current session from the existing thread context instead of opening a new session. You should have configured hibernate.current_session_context_class in the hibernate configuration file to use getCurrentSession method. The same entry looks like this:

<session-factory>
<!--  Other elements goes here -->
<property name="hibernate.current_session_context_class">
          org.hibernate.context.internal.ThreadLocalSessionContext
</property>
</session-factory>

If you are not adding the above configuration in your configuration file, you would get the following exception while getting the session.

Exception in thread "main" org.hibernate.HibernateException: No CurrentSessionContext configured!
	at org.hibernate.internal.SessionFactoryImpl.getCurrentSession(SessionFactoryImpl.java:1010)
	at javabeat.net.hibernate.HibernateUtil.main(HibernateUtil.java:16)

HibernateUtil.java

package javabeat.net.hibernate;

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

public class HibernateUtil {
	public static void main (String args[]){
		Configuration configuration = new Configuration().configure();
		ServiceRegistry registry = new ServiceRegistryBuilder().configure().build();
		SessionFactory sessionFactory  = new MetadataSources(registry)
		.addAnnotatedClass(Employee.class).buildMetadata().buildSessionFactory();
		Session session  = sessionFactory.getCurrentSession();
		//Session session  = sessionFactory.openSession();
	}
}

Category: HibernateTag: Hibernate 4 Tutorials

About Krishna Srinivasan

He is Founder and Chief Editor of JavaBeat. He has more than 8+ years of experience on developing Web applications. He writes about Spring, DOJO, JSF, Hibernate and many other emerging technologies in this blog.

Previous Post: « Hibernate Dependency Library Exception (JTA) : javax/transaction/SystemException
Next Post: Hibernate Exception – org/dom4j/DocumentException »

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Primary Sidebar

Follow Us

  • Facebook
  • Pinterest

FEATURED TUTORIALS

New Features in Spring Boot 1.4

Difference Between @RequestParam and @PathVariable in Spring MVC

What is new in Java 6.0 Collections API?

The Java 6.0 Compiler API

Introductiion to Jakarta Struts

What’s new in Struts 2.0? – Struts 2.0 Framework

JavaBeat

Copyright © by JavaBeat · All rights reserved
Privacy Policy | Contact