Announcement Announcement Module
Collapse
No announcement yet.
Can't get a HibernateInterceptor to work. Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Can't get a HibernateInterceptor to work.

    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.

    Code:
        <bean id="hibernateInterceptor" class="org.springframework.orm.hibernate.HibernateInterceptor">
           <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!

  • #2
    Here's a stack trace:

    Code:
    net.sf.hibernate.LazyInitializationException&#58; Failed to lazily initialize a collection - no session or session was closed
    	net.sf.hibernate.collection.PersistentCollection.initialize&#40;PersistentCollection.java&#58;209&#41;
    	net.sf.hibernate.collection.PersistentCollection.read&#40;PersistentCollection.java&#58;71&#41;
    	net.sf.hibernate.collection.Set.toArray&#40;Set.java&#58;138&#41;
    	java.util.ArrayList.addAll&#40;ArrayList.java&#58;474&#41;
    	java.util.Collections$SynchronizedCollection.addAll&#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;ClaimController.java&#58;32&#41;
    	eha.claim.ClaimController$$FastClassByCGLIB$$2d8f1956.invoke&#40;<generated>&#41;
    	net.sf.cglib.proxy.MethodProxy.invoke&#40;MethodProxy.java&#58;149&#41;
    	org.springframework.aop.framework.Cglib2AopProxy$CglibMethodInvocation.invokeJoinpoint&#40;Cglib2AopProxy.java&#58;676&#41;
    	org.springframework.aop.framework.ReflectiveMethodInvocation.proceed&#40;ReflectiveMethodInvocation.java&#58;122&#41;
    	org.springframework.orm.hibernate.HibernateInterceptor.invoke&#40;HibernateInterceptor.java&#58;163&#41;
    	org.springframework.aop.framework.ReflectiveMethodInvocation.proceed&#40;ReflectiveMethodInvocation.java&#58;144&#41;
    	org.springframework.aop.framework.Cglib2AopProxy$DynamicAdvisedInterceptor.intercept&#40;Cglib2AopProxy.java&#58;619&#41;
    	eha.claim.ClaimController$$EnhancerByCGLIB$$75b1172c.handleEhaRequest&#40;<generated>&#41;
    	eha.util.spring.EHAController.handleRequest&#40;EHAController.java&#58;50&#41;
    	org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter.handle&#40;SimpleControllerHandlerAdapter.java&#58;44&#41;
    	org.springframework.web.servlet.DispatcherServlet.doService&#40;DispatcherServlet.java&#58;584&#41;
    	org.springframework.web.servlet.FrameworkServlet.serviceWrapper&#40;FrameworkServlet.java&#58;368&#41;
    	org.springframework.web.servlet.FrameworkServlet.doGet&#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.

    Comment


    • #3
      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. It's probably better that I grab the object from Hibernate and let Hibernate worry about caching it anyway.

      Comment

      Working...
      X