java - Solve Hibernate Lazy-Init issue with hibernate.enable_lazy_load_no_trans -
i have been suffering infamous hibernate exception
org.hibernate.lazyinitializationexception: not initialize proxy - no session
now community cheering on
<property name="hibernate.enable_lazy_load_no_trans" value="true"/>
saying solves problem use caution.
what mean use caution? property does?
please give me insights. in advance.
the problem approach can have n+1 effect.
imagine have following entity:
public class person{ @onetomany // default lazy private list<order> orderlist; }
if have report returns 10k of persons, , if in report execute code person.getorderlist()
jpa/hibernate execute 10k of queries. n+1 effect, have no control queries executed.
imagine order below:
public class order{ @onetomany // default lazy private list<emailsent> emailsentlist; }
imagine have iteration person.getorderlist()
, every order order
you order.getemailsentlist()
. can see problem now?
for lazyinitializationexception can have solutions:
- use openinsessioninview approach. need create webfilter open , close transaction. problem n+1 effect.
- use hibernate.enable_lazy_load_no_trans configuration, hibernate , not able port project other jpa provider if needed. can have n+1 effect.
- use ejb feature named persistencecontext extended. keep context opened of several transactions. problems are: n+1 effect can happen, use lot of server memory (entities stay managed)
- use fetch in query. approach jpql/hql like:
select p person p join fetch p.orderlist
. query have list loaded database , not have n+1 effect. problem need write jpql each case.
if still have problem, check link: http://uaihebert.com/four-solutions-to-the-lazyinitializationexception
Comments
Post a Comment