Spring: Why does the SessionFactoryUtils class in Hibernate 4 not provide the getSession method?

What happened to the SessionFactoryUtils.getSession method from Hibernate 4 in Spring 3.1.0? What should be used instead? sessionFactory.getCurrentSession () continues to give me this exception:

org.hibernate.HibernateException: No Session found for current thread at org.springframework.orm.hibernate4.SpringSessionContext.currentSession(SpringSessionContext.java:97) at org.hibernate.internal.SessionFactoryImpl.getCurrentSession(SessionFactoryImpl.java:883) 

Any clues?

+7
source share
4 answers

Use sessionFactory.getCurrentSession()

I believe that the concept of "current session" appeared in Hibernate 3, SessionFactoryUtil was written before it existed.

+3
source

I donโ€™t know why they deleted them, maybe they implemented a better design using org.hibernate.context.CurrentSessionContext . How from this post :

... Just use a simple SessionFactory and use the getCurrentSession method to get the current transactional session (don't use openSession !!!!), and you should go ...

As from the documentation of SessionFactory.getCurrentSession () :

 The definition of what exactly "current" means controlled by the CurrentSessionContext impl configured for use. 
0
source

The exception ' org.hibernate.HibernateException: No Session found for current thread ' occurs if you incorrectly configured transaction demarcation using Spring.

Typically, Spring opens a session for you when your transaction begins, binds it to ThreadLocal, and reuses it throughout the transaction. When the transaction completes, the session closes (after it is reset). This way you do not need to make any calls in the SessionFactory class.

Usually people put @Transactional in a class (usually in a service) and configure Spring to start and end transactions when calling a method on this class.

0
source

You need to add an example property:

<prop key="hibernate.current_session_context_class">thread</prop>

to your factory bean session matching. (see sleep mode documentation for more details)

0
source

All Articles