栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

Hibernate/Spring:无法延迟初始化-没有会话或会话已关闭

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Hibernate/Spring:无法延迟初始化-没有会话或会话已关闭

我认为您不应该使用hibernate会话事务方法,而让spring这样做。

将此添加到您的spring conf:

<bean id="txManager" >    <property name="sessionFactory" ref="mySessionFactory" /></bean><bean id="transactionTemplate" >    <property name="transactionManager" ref="txManager"/></bean>

然后我将修改您的测试方法以使用spring事务模板:

public static void main(String[] args) {    // init here (getting dao and transaction template)    transactionTemplate.execute(new TransactionCallback() {        @Override        public Object doInTransaction(TransactionStatus status) {          // do your hibernate stuff in here : call save, list method, etc        }    }}

附带说明一下,@OneToMany关联默认情况下是惰性的,因此您无需将其注释为惰性。(@ * ToMany默认为LAZY,@ *ToOne默认为EAGER)

编辑:从hibernate的角度来看,这就是现在正在发生的事情:

  • 开放会话(从事务开始)
  • 保存用户并将其保留在会话中(请参阅会话缓存作为实体哈希图,其中键是实体ID)
  • 保存事件并将其保留在会话中
  • 保存另一个事件并将其保留在会话中
  • …与所有保存操作相同…

  • 然后加载所有用户(“来自用户”查询)

  • 那时,hibernate看到它在其会话中已经有该对象,因此丢弃从请求中获取的对象,然后从会话中返回该对象。

  • 您会话中的用户未初始化其事件集合,因此您将获得null。

以下是增强代码的一些要点:

  • 在模型中,当不需要集合排序时,对集合使用Set而不是List(私有Set事件,而不是私有List事件)
  • 在模型中,键入您的集合,否则hibernate将不会获取哪个实体(私有Set 事件)
  • 当您设置双向关系的一侧,并且希望在同一事务中使用该关系的mappedBy一侧时,请设置双方。Hibernate不会在下一次发送之前为您执行此操作(当会话是从db状态返回的新视图时)。

因此,要解决上述问题,可以在一个事务中进行保存,而在另一事务中进行加载:

public static void main(String[] args) {    // init here (getting dao and transaction template)    transactionTemplate.execute(new TransactionCallback() {        @Override        public Object doInTransaction(TransactionStatus status) {          // save here        }    }    transactionTemplate.execute(new TransactionCallback() {        @Override        public Object doInTransaction(TransactionStatus status) {          // list here        }    }}

或设置双方:

...event1.setUser(user);...event2.setUser(user);...user.setEvents(Arrays.asList(event1,event2));...

(也别忘了解决上面的代码增强点,“不设置列表,集合键入”)



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/440273.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号