This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan 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 Android Security Essentials Live Lessons this week in the Android 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
Author

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:

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

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

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
Greenhorn

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"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="configLocation">
<value>file:src/hibernate.cfg.xml</value>
</property>
</bean>
[ 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(BytecodeProviderImpl.java:49)
at org.hibernate.tuple.entity.PojoEntityTuplizer.buildProxyFactoryInternal(PojoEntityTuplizer.java:203)
at org.hibernate.tuple.entity.PojoEntityTuplizer.buildProxyFactory(PojoEntityTuplizer.java:181)
at org.hibernate.tuple.entity.AbstractEntityTuplizer.<init>(AbstractEntityTuplizer.java:158)
at org.hibernate.tuple.entity.PojoEntityTuplizer.<init>(PojoEntityTuplizer.java:76)
at org.hibernate.tuple.entity.EntityEntityModeToTuplizerMapping.<init>(EntityEntityModeToTuplizerMapping.java:80)
at org.hibernate.tuple.entity.EntityMetamodel.<init>(EntityMetamodel.java:325)
at org.hibernate.persister.entity.AbstractEntityPersister.<init>(AbstractEntityPersister.java:457)
at org.hibernate.persister.entity.SingleTableEntityPersister.<init>(SingleTableEntityPersister.java:131)
at org.hibernate.persister.PersisterFactory.createClassPersister(PersisterFactory.java:84)
at org.hibernate.impl.SessionFactoryImpl.<init>(SessionFactoryImpl.java:261)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1327)
at org.hibernate.cfg.AnnotationConfiguration.buildSessionFactory(AnnotationConfiguration.java:867)
at org.springframework.orm.hibernate3.LocalSessionFactoryBean.newSessionFactory(LocalSessionFactoryBean.java:805)
at org.springframework.orm.hibernate3.LocalSessionFactoryBean.buildSessionFactory(LocalSessionFactoryBean.java:745)
at org.springframework.orm.hibernate3.AbstractSessionFactoryBean.afterPropertiesSet(AbstractSessionFactoryBean.java:134)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1202)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1172)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:428)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:251)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:156)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:248)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:160)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:284)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:352)
at org.springframework.web.context.ContextLoader.createWebApplicationContext(ContextLoader.java:244)
at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:187)
at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:49)
at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4623)
at org.apache.catalina.core.StandardContext.start(StandardContext.java:5323)
at com.sun.enterprise.web.WebModule.start(WebModule.java:456)
at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:922)
at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:906)
at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:696)
at com.sun.enterprise.web.WebContainer.loadWebModule(WebContainer.java:2205)
at com.sun.enterprise.web.WebContainer.loadWebModule(WebContainer.java:1890)
at com.sun.enterprise.web.WebApplication.start(WebApplication.java:85)
at com.sun.enterprise.v3.server.ApplicationLifecycle.start(ApplicationLifecycle.java:560)
at com.sun.enterprise.v3.server.ApplicationLifecycle.start(ApplicationLifecycle.java:547)
at com.sun.enterprise.v3.server.ApplicationLifecycle.deploy(ApplicationLifecycle.java:
upon following
Lee Mark
Ranch Hand

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

Julius Fernandes
Greenhorn

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: http://aspose.com/file-tools
 
subject: How to define Hibernate SessionFactory with Spring applicationcontext.xml
 
Similar Threads
spring 2 with hibernate 3
createSQLQuery in Spring
Spring 2.5 - Rolling back first method, when the second method throws Runtime ex
Struts 2 + Spring 3 +Hibernate 3.5 Integration Problem session factory nullpointer exception
LocalSessionFactoryBean is not a sessionfactory instance