Hibernate Too Lazy -


i have single parent/child @onetomany collection set (no dups). below error if try access child parent on server.

if pass parent client side via request.setattribute , access parent using jstl, can access child objects members directly. parent/childs have getters/setters.

i'm using tomcat 6.0.32, spring 3.1.0, jdk6, hibernate 3.

parent

@entity @table(name="suser") public class suser {      @id     @generatedvalue(strategy = generationtype.identity)     private int su_id;      @column(name="displayname", nullable=true,      columndefinition="varchar", length=50, insertable=true, updatable=true)     private string displayname;      @column(name="last_activity", nullable=true, columndefinition="datetime",      insertable=true, updatable=true)     private string last_activity;      @column(name="ldapuser", nullable=true, columndefinition="varchar",      length=50, insertable=true, updatable=true)     private string ldapuser;      @onetomany     @org.hibernate.annotations.indexcolumn(name="su_id")     @joincolumn(name="su_id", insertable=true, updatable=true)     private set<suserattributes> suattr = new hashset<suserattributes>(); 

child

@entity @table(name="suser_attributes") public class suserattributes {      @id     @generatedvalue(strategy = generationtype.identity)     private int sua_id;      @column(name="su_id")     private int su_id;      @column(name="sua_key", nullable=true, columndefinition="varchar",      length=64, insertable=true, updatable=true)     private string sua_key;      @column(name="sua_value", nullable=true, columndefinition="varchar",      length=128, insertable=true, updatable=true)     private string sua_value; 

dao

package com.oasis.implementation.dao;  import java.util.linkedlist; import java.util.list;  import org.apache.log4j.logger; import org.hibernate.sqlquery; import org.hibernate.session; import org.hibernate.sessionfactory; import org.hibernate.criterion.restrictions; import org.springframework.beans.factory.annotation.autowired; import org.springframework.beans.factory.annotation.qualifier;  import com.oasis.implementation.clientinfo; import com.oasis.implementation.suser;  public class suserdaoimpl implements suserdao {      @suppresswarnings("unused")     private static logger logger = logger             .getlogger(clientdownloadsdaoimpl.class);      @autowired     @qualifier("implementationsessionfactory")     private sessionfactory sessionfactory;      @suppresswarnings("unchecked")     @override     public list<suser> getallsusers(){         list<suser> sulist = null;         session session = null;          try         {             session = sessionfactory.opensession();             session.begintransaction();             sulist = session.createquery("from suser").list();                       session.gettransaction().commit();         }         catch (runtimeexception e)         {                        system.out.println (e.getmessage());         }                    {             if (session != null)             {                            session.close();             }            }                 return sulist;            }      @override     public list<suser> getsuserbyid(int su_id) {         session session = null;         suser suser = null;         linkedlist<suser> sulist = new linkedlist<suser>();          try {             session = sessionfactory.opensession();             session.begintransaction();             suser = (suser) session.get(suser.class, su_id);             sulist.add(suser);             session.gettransaction().commit();         } catch (runtimeexception e) {             system.out.println(e.getmessage());         } {             if (session != null) {                 session.close();             }         }          return sulist;     }      @override     public list<suser> getuserbyldap(string ldapuser){         session session = null;         int user_id = 0;         list<suser> sulist = null;          try         {             session = sessionfactory.opensession();             session.begintransaction();              sqlquery sqlq = session.createsqlquery("select su_id              suser ldapuser = '" + ldapuser + "'");             user_id = (integer)sqlq.uniqueresult();             sulist = getsuserbyid(user_id);              session.gettransaction().commit();         }         catch (runtimeexception e)         {                        system.out.println (e.getmessage());         }                    {             if (session != null)             {                            session.close();             }            }                 return sulist;            } } 

error message

    hibernate: select suser0_.su_id su1_11_0_, suser0_.displayname  displayn2_11_0_, suser0_.last_activity last3_11_0_, suser0_.ldapuser l     dapuser11_0_ suser suser0_ suser0_.su_id=?     error - failed lazily initialize collection of role: com.oasis.implementation.suser.suattr, no session or session closed     org.hibernate.lazyinitializationexception: failed lazily initialize collection of role: com.oasis.implementation.suser.suattr, no session or      session closed             @  

this error happening because you're trying access lazily loaded collection (that means collection hasn't been fetched db yet) once hibernate session closed. so, no means of getting db fetch children. either:

  • force-load association when session still open, call hibernate.initialize(parent.getsuattr());

  • create specific query use case explicit join fetch statements.

  • modify mapping eagerly fetch association if you're going query children whenever query parent.

  • adopt opensessioninview pattern. pattern opens hibernate session when request processed (usually in filter), , keeps open until has been processed. take account approach might not suitable current design (can require redesign), , should used carefully, has pitfalls can mess things up.


Comments

Popular posts from this blog

javascript - Count length of each class -

What design pattern is this code in Javascript? -

hadoop - Restrict secondarynamenode to be installed and run on any other node in the cluster -