hibernate延迟加载org

2019-10-11 21:23栏目:王中王今日开奖结果
TAG:

public static void main(String[] args) {
 
  DeptEntity dept = getDept("402882e762ae888d0162ae888e420000");

  //dept.getEmp()获得子表的记录会集
  System.out.println(dept.getEmp());

}

private static DeptEntity getDept(String did){
  Session session = sessionFactory.openSession();
  DeptEntity dept = (DeptEntity)session.get(DeptEntity.class, did);
  session.close();
  return dept;
}

 

运转结果:

Exception in thread "main" org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: com.javakc.hibernate.onetomany.entity.DeptEntity.emp, could not initialize proxy - no Session
at org.hibernate.collection.internal.AbstractPersistentCollection.throwLazyInitializationException(AbstractPersistentCollection.java:566)
at org.hibernate.collection.internal.AbstractPersistentCollection.withTemporarySessionIfNeeded(AbstractPersistentCollection.java:186)
at org.hibernate.collection.internal.AbstractPersistentCollection.initialize(AbstractPersistentCollection.java:545)
at org.hibernate.collection.internal.AbstractPersistentCollection.read(AbstractPersistentCollection.java:124)
at org.hibernate.collection.internal.PersistentSet.toString(PersistentSet.java:326)
at java.lang.String.valueOf(String.java:2827)
at java.io.PrintStream.println(PrintStream.java:771)
at com.javakc.hibernate.onetomany.action.TestAction.main(TestAction.java:74)

 

晤面延迟加载起初化失利,无法发轫化三个代理。便是汇聚在非一对一对象关系中,为了节约财富是私下认可延迟加载,而get方法又是非延迟加载,所以在实行完贰遍数据库查询后就实行session.close();关闭了session,而集合是延迟加载,在使用集适时再加载,此时session已经关门,所以得不到代办。化解办法:可以在主表的hbm配置文件中,在<set>标签里设置lazy="false",集结就不延迟加载了,因而在进行get方法时,集合也取得到了,就不会出现延迟加载难题了。

版权声明:本文由493333王中王开奖结果发布于王中王今日开奖结果,转载请注明出处:hibernate延迟加载org