java - JSF + Hibernate: Collection is not associated with any session -
first of all, use java ee, hibernate entitymanager , primefaces.
i have 1 ejb module (business logic , domain) , 2 war modules (jersey ws , jsf primefaces).
i decided initialize lazy collections in jsf war module avoid lazy initialization exception. don't use extended entity manager.
@managedbean(name = "company") @sessionscoped public class companybean { @ejb private companyfacade cf; ... public string showdetails(long id) { company = cf.find(id); hibernate.initialize(company.getcompanytypes()); hibernate.initialize(company.getprimaryuser()); hibernate.initialize(company.getblocked()); hibernate.initialize(company.getaddresses()); hibernate.initialize(company.getcontacts()); return "details"; } ... }
and get:
caused by: org.hibernate.hibernateexception: collection not associated session @ org.hibernate.collection.abstractpersistentcollection.forceinitialization(abstractpersistentcollection.java:474) @ org.hibernate.hibernate.initialize(hibernate.java:417) @ minepackage.companybean.showdetails(companybean.java:79) ...
i don't understand it. there has session when 1 line before initialization fetched database, doesn't it? initialize attributes in ws module in similar way , there it's working.
any idea what's happening?
i think session closed after ejb finished, objects in detached state. hibernate.initialize()
won't work more. have multiple options here:
- open transaction on client side (in jsf bean or in servlet filter). way session still open when calling
hibernate.initialize()
. - modify ejb load full object , required collections. use fetch joins and/or use
hibernate.initialize()
there. - create more fine grained api in ejb. method
companyfacade.getaddressesbycompany()
.
i prefer combination of latter two. use fetch joins load one-to-one , many-to-one relationships in find
method , add methods loading one-to-many collections (like addresses). improve performance of backend because reduces number of database queries.
Comments
Post a Comment