This week's book giveaway is in the OCAJP 8 forum.
We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line!
See this thread for details.
The moose likes Other Application Frameworks and the fly likes How to define Hibernate SessionFactory with Spring applicationcontext.xml Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of OCA Java SE 8 Programmer I Study Guide this week in the OCAJP 8 forum!
JavaRanch » Java Forums » Frameworks » Other Application Frameworks
Bookmark "How to define Hibernate SessionFactory with Spring applicationcontext.xml" Watch "How to define Hibernate SessionFactory with Spring applicationcontext.xml" New topic

How to define Hibernate SessionFactory with Spring applicationcontext.xml

Jane Somerfield
Ranch Hand

Joined: Jul 20, 2002
Posts: 193
The following section is from reference document of "Spring Java/J2EE Application Framework 2.0.2"
by Rod Johnson etc. on page 212.

12.2.5. Implementing DAOs based on plain Hibernate3 API

Hibernate 3.0.1 introduced a feature called "contextual Sessions", where Hibernate itself manages one current
Session per transaction. This is roughly equivalent to Spring's synchronization of one Hibernate Session per
transaction. A corresponding DAO implementation looks like as follows, based on the plain Hibernate API:

public class ProductDaoImpl implements ProductDao
private SessionFactory sessionFactory;
public void setSessionFactory(SessionFactory sessionFactory)
this.sessionFactory = sessionFactory;
public Collection loadProductsByCategory(String category)
return this.sessionFactory.getCurrentSession().createQuery("from test.Product product where product.category=?").setParameter(0, category).list();

This style is very similar to what you will find in the Hibernate reference documentation and examples, except
for holding the SessionFactory in an instance variable. We strongly recommend such an instance-based setup
over the old-school static HibernateUtil class from Hibernate's CaveatEmptor sample application. (In
general, do not keep any resources in static variables unless absolutely necessary.)
The above DAO follows the Dependency Injection pattern: it fits nicely into a Spring IoC container, just like it
would if coded against Spring's HibernateTemplate. Of course, such a DAO can also be set up in plain Java
(for example, in unit tests): simply instantiate it and call setSessionFactory(..) with the desired factory
reference. As a Spring bean definition, it would look as follows:

<bean id="myProductDao" class="product.ProductDaoImpl">
<property name="sessionFactory" ref="mySessionFactory"/>

The main advantage of this DAO style is that it depends on Hibernate API only;
no import of any Spring class is required.

My question is how to configure sessionFactory in applicationcontext.xml.
I know the sessionFactory is the type of "org.hibernate.SessionFactory",
not "org.springframework.orm.hibernate3.LocalSessionFactoryBean". The reference
document is not clear on this.
rushi kumar

Joined: Jul 24, 2007
Posts: 10
Spring provides integration to hibernate,so it provides LocalSessionFactoryBean class.According to your example ,U can configure in applicationContext.xml like this

<bean id="mySessionFactory"
<property name="configLocation">
[ August 28, 2007: Message edited by: rushi kumar ]
Lee Mark
Ranch Hand

Joined: Mar 05, 2009
Posts: 46
i am getting following exception
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in class path resource [DAOContext.xml]: Invocation of init method failed; nested exception is java.lang.NoClassDefFoundError: javassist/util/proxy/MethodFilter
Caused by: java.lang.NoClassDefFoundError: javassist/util/proxy/MethodFilter
at org.hibernate.bytecode.javassist.BytecodeProviderImpl.getProxyFactoryFactory(
at org.hibernate.tuple.entity.PojoEntityTuplizer.buildProxyFactoryInternal(
at org.hibernate.tuple.entity.PojoEntityTuplizer.buildProxyFactory(
at org.hibernate.tuple.entity.AbstractEntityTuplizer.<init>(
at org.hibernate.tuple.entity.PojoEntityTuplizer.<init>(
at org.hibernate.tuple.entity.EntityEntityModeToTuplizerMapping.<init>(
at org.hibernate.tuple.entity.EntityMetamodel.<init>(
at org.hibernate.persister.entity.AbstractEntityPersister.<init>(
at org.hibernate.persister.entity.SingleTableEntityPersister.<init>(
at org.hibernate.persister.PersisterFactory.createClassPersister(
at org.hibernate.impl.SessionFactoryImpl.<init>(
at org.hibernate.cfg.Configuration.buildSessionFactory(
at org.hibernate.cfg.AnnotationConfiguration.buildSessionFactory(
at org.springframework.orm.hibernate3.LocalSessionFactoryBean.newSessionFactory(
at org.springframework.orm.hibernate3.LocalSessionFactoryBean.buildSessionFactory(
at org.springframework.orm.hibernate3.AbstractSessionFactoryBean.afterPropertiesSet(
at org.springframework.web.context.ContextLoader.createWebApplicationContext(
at org.springframework.web.context.ContextLoader.initWebApplicationContext(
at org.springframework.web.context.ContextLoaderListener.contextInitialized(
at org.apache.catalina.core.StandardContext.listenerStart(
at org.apache.catalina.core.StandardContext.start(
at com.sun.enterprise.web.WebModule.start(
at org.apache.catalina.core.ContainerBase.addChildInternal(
at org.apache.catalina.core.ContainerBase.addChild(
at org.apache.catalina.core.StandardHost.addChild(
at com.sun.enterprise.web.WebContainer.loadWebModule(
at com.sun.enterprise.web.WebContainer.loadWebModule(
at com.sun.enterprise.web.WebApplication.start(
at com.sun.enterprise.v3.server.ApplicationLifecycle.start(
at com.sun.enterprise.v3.server.ApplicationLifecycle.start(
at com.sun.enterprise.v3.server.ApplicationLifecycle.deploy(
upon following
Lee Mark
Ranch Hand

Joined: Mar 05, 2009
Posts: 46
DAOcontext.xml file

Julius Fernandes

Joined: Oct 10, 2004
Posts: 2
It means that it failed to load a class. I believe you are missing the javaassist.jar file in your classpath
I agree. Here's the link:
subject: How to define Hibernate SessionFactory with Spring applicationcontext.xml
It's not a secret anymore!