spring - hibernate with ehcache not working well -


i'm using spring 4, hibernate 4 project. recently, added ehcache project. there entity com.xxx.employee holds entity com.xxx.user. used hibernatetemplate findbycriteria(detachedcriteria, int, int) execute query, , then, make query via hibernatetemplate get(entityname, id), there no sql shown in logging, seems second cache works, however, user of employee missing, there "org.hibernate.lazyinitializationexception: not initialize proxy - no session" thrown.

is there additional setting need add save object's object value cache?

<hibernate-mapping package="com.xxx"> <class name="employee" table="t_employee">     <cache usage="read-write" include="all"/>     <id name="personid" column="person_id">         <generator class="identity" />     </id>      <property name="name" column="name" />      <many-to-one name="department" column="department_id" fetch="join" not-null="false" />     <many-to-one name="user" column="user_id" fetch="join" not-null="false" cascade="all-delete-orphan" unique="true"/>  </class> 

<hibernate-mapping package="com.xxx"> <class name="user" table="t_user">     <cache usage="read-write"/>     <id name="userid" column="user_id">         <generator class="identity" />     </id>     <property name="name" column="name" not-null="true" />     <property name="loginname" column="login_name" unique="true" not-null="true" />     <property name="password" column="password" not-null="true"  />     <many-to-one name="employee" column="employee_id" fetch="join" not-null="false" />     <many-to-one name="role" class="role" column="role_id" fetch="join" not-null="true" />     <property name="lock" column="col_lock" type="integer" />     <property name="createdate" column="create_date" />     <property name="updatedate" column="update_date" />     <property name="passwordupdatedate" column="pwd_update_date" />     <property name="logintimes" column="login_times" type="integer" />     <property name="lastlogindate" column="last_login_date" />     <property name="comment" column="comment" />  </class> 

public t getonebyid(serializable id) throws tamsexception {     if (id == null) {         logger.error("getonebyid id null");         throw new tamsexception("getonebyid id null");     }     t t;     try {         log.info("get " + getentityname() + " id : " + id);          hibernatetemplate ht = gethibernatetemplate();         ht.setcachequeries(true);         t = (t) ht.get(getentityname(), id);         return t;     } catch (exception e) {         throw new tamsexception("get " + getentityname() + ":  failure!", e);     } {         log.info("get " + getentityname() + " id : " + id + " success!");     } }   public list<t> querybycondition(querycondition condition)         throws tamsexception {      log.info("query " + getentityname() + " by: " + condition + " ...");     try {         int firstresult = 0;         int maxresults = 0;          detachedcriteria c = detachedcriteria                 .forentityname(getentityname());          if (condition != null) {             firstresult = condition.getfirstresult();             maxresults = condition.getmaxresults();             assemblecriteria(condition, c);         }         hibernatetemplate ht = gethibernatetemplate();         ht.setcachequeries(true);         @suppresswarnings("unchecked")         list<t> list = (list<t>) ht.findbycriteria(c, firstresult,                 maxresults);          log.info("query " + getentityname() + " by: " + condition                 + " ret size --> " + list.size());         return list;     } catch (exception e) {         throw new tamsexception("query " + getentityname() + " by: "                 + condition + " failure!", e);     }  } 

you receive org.hibernate.lazyinitializationexception: not initialize proxy - no session error, because property employee.user configured option lazy=proxy (this default, when not mentioned).

here piece documentation:

lazy (optional - defaults proxy): default, single point associations proxied. lazy="no-proxy" specifies property should fetched lazily when instance variable first accessed. requires build-time bytecode instrumentation. lazy="false" specifies association eagerly fetched.

so, you've got 2 choices:

  1. use lazy=false eagerly fetch association
  2. open session before working required associations

Comments

Popular posts from this blog

java - Spring Data JPA: Why findOne(id) executing delete query internally? -

python - Mongodb How to add addtional information when aggregating? -

java - Incorrect order of records in M-M relationship in hibernate -