PDA

View Full Version : Can't get a HibernateInterceptor to work.



wiggles
Feb 23rd, 2005, 03:02 PM
I use a HibernateTemplate in my DAO, but I still need to use AOP so that my objects can lazy load throught the application. Here's how I have my application context set up. The controller is where code is called that accesses lazily loaded data, not the DAO.



<bean id="hibernateInterceptor" class="org.springframework.orm.hibernate.HibernateInterce ptor">
<property name="sessionFactory"><ref bean="sessionFactory"/></property>
</bean>

<bean id="controllerTarget" class="com.website.MyController">
<property name="dataAccessObject"><ref bean="dao"/></property>
</bean>
<bean id="controller" class="org.springframework.aop.framework.ProxyFactoryBean">
<property name="target"><ref bean="controllerTarget"/></property>
<property name="interceptorNames">
<list>
<value>hibernateInterceptor</value>
</list>
</property>
</bean>


Now, when I run MyController, I get a non-existant Hibernate session exception. Can anyone see any obvious problems? Thanks for the help!

wiggles
Feb 23rd, 2005, 03:19 PM
Here's a stack trace:



net.sf.hibernate.LazyInitializationException&#58; Failed to lazily initialize a collection - no session or session was closed
net.sf.hibernate.collection.PersistentCollection.i nitialize&#40;PersistentCollection.java&#58;209&#41;
net.sf.hibernate.collection.PersistentCollection.r ead&#40;PersistentCollection.java&#58;71&#41;
net.sf.hibernate.collection.Set.toArray&#40;Set.java&#58;1 38&#41;
java.util.ArrayList.addAll&#40;ArrayList.java&#58;474&#41;
java.util.Collections$SynchronizedCollection.addAl l&#40;Collections.java&#58;1591&#41;
eha.util.Table.addAll&#40;Table.java&#58;52&#41;
eha.util.Table.<init>&#40;Table.java&#58;44&#41;
eha.claim.ClaimController.handleEhaRequest&#40;ClaimCo ntroller.java&#58;32&#41;
eha.claim.ClaimController$$FastClassByCGLIB$$2d8f1 956.invoke&#40;<generated>&#41;
net.sf.cglib.proxy.MethodProxy.invoke&#40;MethodProxy. java&#58;149&#41;
org.springframework.aop.framework.Cglib2AopProxy$C glibMethodInvocation.invokeJoinpoint&#40;Cglib2AopProx y.java&#58;676&#41;
org.springframework.aop.framework.ReflectiveMethod Invocation.proceed&#40;ReflectiveMethodInvocation.java &#58;122&#41;
org.springframework.orm.hibernate.HibernateInterce ptor.invoke&#40;HibernateInterceptor.java&#58;163&#41;
org.springframework.aop.framework.ReflectiveMethod Invocation.proceed&#40;ReflectiveMethodInvocation.java &#58;144&#41;
org.springframework.aop.framework.Cglib2AopProxy$D ynamicAdvisedInterceptor.intercept&#40;Cglib2AopProxy. java&#58;619&#41;
eha.claim.ClaimController$$EnhancerByCGLIB$$75b117 2c.handleEhaRequest&#40;<generated>&#41;
eha.util.spring.EHAController.handleRequest&#40;EHACon troller.java&#58;50&#41;
org.springframework.web.servlet.mvc.SimpleControll erHandlerAdapter.handle&#40;SimpleControllerHandlerAda pter.java&#58;44&#41;
org.springframework.web.servlet.DispatcherServlet. doService&#40;DispatcherServlet.java&#58;584&#41;
org.springframework.web.servlet.FrameworkServlet.s erviceWrapper&#40;FrameworkServlet.java&#58;368&#41;
org.springframework.web.servlet.FrameworkServlet.d oGet&#40;FrameworkServlet.java&#58;319&#41;
javax.servlet.http.HttpServlet.service&#40;HttpServlet .java&#58;689&#41;
javax.servlet.http.HttpServlet.service&#40;HttpServlet .java&#58;802&#41;
eha.filter.SiteFilter.doFilter&#40;SiteFilter.java&#58;124 &#41;


EDIT: So, it looks like everything is getting called properly. Just for kicks, I wrapped the offending line in HibernateTemplate.execute() and still got the error. hehe, lazy loading just seems impossible for me. :D

wiggles
Feb 24th, 2005, 11:46 AM
Okay, I think I may know what the problem is. Does the lazy initialization need to happen within the same session that the object was created? Because I'm storing an object in the session, then trying to lazy load it during a different page load.

EDIT: Woo hoo! That's it. :D It's probably better that I grab the object from Hibernate and let Hibernate worry about caching it anyway.