spring - Mantain long Transaction Session in Spring3 and Hibernate4 -
i lazyinitializationexception shop.events collection in below scenario.
i know problem might transaction session closed before call shop.getevents executed.
i learning opensessioninviewfilter, think no idea mantain every transaction sessions in every calls life server. , fetchtype.eager not neither.
i need solve question. thank in advance.
@entity @table(name = "shop") public class shop implements serializable { // class attributes. @onetomany(mappedby = "shop", fetch=fetchtype.lazy) private set<event> events; // getters , setters. }
@entity @table(name = "event") public class event implements serializable { // class attributes. @manytoone(fetch = fetchtype.lazy) @joincolumn(name = "shop_id", nullable = false) private shop shop; // getters , setters. }
persistence layer implementation.
public interface abstractdao<e, extends serializable> { e finduniquebycriteria(criteria criteria); }
public interface shopdao extends abstractdao<shop, string>{ shop getshopfromid(int shop_id, int manager_id); }
@repository("shopdao") public class shopdaoimpl extends abstractdaoimpl<shop, string> implements shopdao { protected shopdaoimpl() { super(shop.class); } @override public shop getshopfromid(int shop_id, int manager_id) { criteria criteria = this.getcurrentsession().createcriteria(shop.class) .add(restrictions.and( restrictions.like("active", true), restrictions.like("id", shop_id))) .createcriteria("manager").add( restrictions.like("id", manager_id)); return (shop) this.finduniquebycriteria(criteria); } }
public interface shopservice { shop getshopfromid(int shop_id, int manager_id); }
@service("shopservice") @transactional(readonly = true) public class shopserviceimpl implements shopservice { @autowired private shopdao shopdao; @override public shop getshopfromid(int shop_id, int manager_id) { return this.shopdao.getshopfromid(shop_id, manager_id); } }
controller this.
class attributes.
@autowired private shopservice shopservice;
method controller.
manager manager = (manager) request.getsession(false).getattribute("manager"); shop shop = (shop) this.shopservice.getshopfromid(shop_id, manager.getid()); set<event> events = shop.getevents();
solution configure controller method @transactional(propagation = propagation.required). thereby use same transaction session in method persistence operations.
Comments
Post a Comment