0

So I realize a Java EE web application (PrimeFaces, Spring, Hibernate) during advancement in programming, I found that all the association between class type Lazy does not always work ("error-type session was closed "..)

So I am forced to work with each fetch eager every time to elimenate this error. I think my work is not good

they told me that spring does not allow Lazy mode, and if I want to work with lazy fashion must add (listener or filter) in the web.xml. Is this true?

Can you give me an example using eager loading?

Arjan Tijms
  • 37,782
  • 12
  • 108
  • 140
salvador
  • 101
  • 5
  • 14
  • 3
    You have to understand how hibernate manages its sessions. When you have `Lazy`, it will try to load objects only when they are needed, but if the session is closed, there is no way to retrieve them. Look into [`PersistenList`](http://docs.jboss.org/hibernate/orm/3.2/api/org/hibernate/collection/PersistentList.html) for example, or how Hibernate wraps your objects in proxies. – Sotirios Delimanolis May 10 '13 at 15:10
  • 1
    http://stackoverflow.com/questions/578433/how-to-solve-lazy-initialization-exception-using-jpa-and-hibernate-as-provider – Patison May 10 '13 at 15:18
  • 1
    @vasileusky I wouldnt sugest that, it could come back to bite you in the ass, it still has some serious bugs and its not recomended for production enviroment. Try OEMIVFilter instead. – Ziul May 10 '13 at 15:29
  • Think You all I try to do it with OpenEntityManagerInViewFilter http://andreazzolini.com/2011/07/27/OpenEntityManagerInViewFilter-and-LazyInitializationException.html – salvador May 10 '13 at 15:55

1 Answers1

1

So. To obtain "lazy" data in view you can add to application context special interceptor:

<mvc:interceptors>
    <bean
        class="org.springframework.orm.hibernate4.support.OpenSessionInViewInterceptor">
        <property name="sessionFactory" ref="sessionFactory" />
    </bean>
</mvc:interceptors>

or if you don't afraid you can modify entityManagerFactory by adding new property:

<property name="jpaProperties">
    <props>
        <prop key="hibernate.enable_lazy_load_no_trans">true</prop>
    </props>
</property>

but it's not recommended because it's still full of bugs

Patison
  • 2,591
  • 1
  • 20
  • 33
  • 1
    Read this discussion: http://stackoverflow.com/questions/15235633/why-is-hibernate-deleting-my-collection-entries-when-all-i-do-is-a-list It only appears to work correctly in hibernate 4.1.7, and even so, there is some things to be aware of. The recommendation to not use in production enviroment yet, comes from the Hibernate team. – Ziul May 10 '13 at 15:59
  • Thx aigain @vasileusky (I'm not using JPA either Spring MVC) just Hibernate 4 and Spring 3 ..could this work – salvador May 10 '13 at 15:59
  • So I summarize I must add the filter entityManagerInViewFilter org.springframework.orm.jpa.support.OpenEntityManagerInViewFilter entityManagerFactoryBeanName entityManagerFactory entityManagerInViewFilter /* REQUEST – salvador May 10 '13 at 16:02