当前位置:王中王鉄算盘开奖结果 > 操作系统 > hibernate延迟加载org

hibernate延迟加载org

文章作者:操作系统 上传时间:2019-10-14

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方法时,集合也博获得了,就不汇合世延迟加载难题了。

本文由王中王鉄算盘开奖结果发布于操作系统,转载请注明出处:hibernate延迟加载org

关键词: