I'm using Spring 4.2.1
and Hiberante 5
and now trying to understand how spring initializes the Session declared in the Spring Beans Definition as follows:
<bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
<!-- properties -->
</bean>
I found that so called org.springframework.orm.hibernate5.LocalSessionFactoryBean implements FactoryBean<SessionFactory>
. Taking this into account it's clear why we define the SessionFactory
definition to be injected with the class org.springframework.orm.hibernate5.LocalSessionFactoryBean
but end up with the instance of SessionFactory
. Now, what I was confused by is the method getCurrentSession:
public Session getCurrentSession() throws HibernateException {
if ( currentSessionContext == null ) {
throw new HibernateException( "No CurrentSessionContext configured!" );
}
return currentSessionContext.currentSession();
}
which delegates the actual session creation to the SpringSessionContext and in my case it's being retrieved by this piece of code:
SessionHolder sessionHolder = (SessionHolder) value;
Session session = sessionHolder.getSession();
But the session
is actually an instance of org.hibernate.internal.SessionImpl
the direct base class org.hibernate.internal.AbstractSessionImpl
of which itself has the property protected transient SessionFactoryImpl factory.
So, the SessionFactory
has the CurrentSessionContext
property which in my case holds SessionHolder
which in turn holds the actual Session
instance. But the SessionImpl
again has a property of the type SessionFactory
.
I cannot understand the circular. Couldn't you explain it a bit.