本文介绍了在Spring中使用jpa / hibernate时,如何保持会话打开以避免延迟初始化异常?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

























$如果我将该集合作为延迟加载离开,我该如何保持会话打开?我曾想过尝试@Transactional,但即使这样做,我也不想这么做,因为通过一个很长的方法让一个事务打开并不合适。

解决方案



基本上,你有几个选择。

- 您可以使用open session in view模式,在服务器端逻辑开始时使用过滤器/拦截器/ AOP式逻辑来打开会话,关闭它。



- 您可以实施跨越多个请求 - 响应周期的对话。



旧的Servlet过滤器是最简单的。

I currently mark collections in entity beans as eager to avoid getting a lazy initialization exception when I try to access the collection properties after loading the bean with the EntityManager.

If I instead leave the collection as lazy loading, how do I keep a session open? I thought about trying @Transactional, but even if that worked I wouldn't want to do it because it doesn't seem right to leave a transaction open over a long method.

解决方案

https://www.hibernate.org/43.html

Basically, you have a few options.

-You can use the "open session in view" pattern where you use a filter/interceptor/AOP - style logic to open a session when server side logic begins, and close it when it's through.

-You could implement conversations spanning several request-response cycles.

A plain old Servlet Filter is the easiest.

这篇关于在Spring中使用jpa / hibernate时,如何保持会话打开以避免延迟初始化异常?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-29 14:27