简体   繁体   English

Hibernate JPA,Spring MVC和LazyInitializationException

[英]Hibernate JPA, Spring MVC and LazyInitializationException

When trying to get items from db, I got this error: 当尝试从数据库获取项目时,出现此错误:

13:00:13.876 [7838526@qtp-204712603-0] ERROR o.h.LazyInitializationException - failed to lazily initialize a collection of role: bo.myobj, no session or session was closed
org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: de.myob.linkedstuff, no session or session was closed

I understand that with switching to eager instead of lazy loading solves this problem, eg 我知道切换到eager而不是lazy loading可以解决此问题,例如

@OneToMany(mappedBy = "myobj", cascade = CascadeType.ALL, fetch=FetchType.EAGER)

and I also understand that eager loading is discouraged. 而且我也明白不鼓励急于加载。 What is the best practitce in order to cope with this issue? 为了解决这个问题,最佳实践是什么?

I am pretty sure this occurs when there is no active transaction. 我很确定在没有活动交易时会发生这种情况。

Read the spring reference part about Declarative Transaction Management 阅读有关声明式事务管理的春季参考部分。

Usually it boils down to your service method or class needing the @Transactional annotation if you use annotations or otherwise proper xml configuration of <tx:advice> . 如果使用批注或<tx:advice>正确xml配置,通常会归结为需要@Transactional批注的服务方法或类。

This is a common problem, usually caused by rendering the view after the hibernate Session is closed. 这是一个常见问题,通常是由休眠Session关闭后呈现视图引起的。 A common solution is to use the Open Session In View pattern, which will keep the hibernate session open for the lifetime of the web request. 常见的解决方案是使用“ 在视图中打开会话”模式,该模式将在Web请求的整个生命周期中保持休眠会话的打开状态。

Spring comes with a filter that implements this pattern. Spring带有实现此模式的过滤器 To enable it for all JSP files in your application, for example, add something like this to your web.xml : 例如,要为您的应用程序中的所有JSP文件启用它,请将类似以下内容添加到您的web.xml

<filter>
    <filter-name>osivFilter</filter-name>
    <filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>
</filter>

<filter-mapping>
    <filter-name>osivFilter</filter-name>
    <url-pattern>*.jsp</url-pattern>
</filter-mapping>

Better separation between the persistence and other layers. 持久层和其他层之间的更好隔离。 Make sure the objects produced by the persistence layer do not include any reference to Hibernate. 确保持久层产生的对象不包含对Hibernate的任何引用。

The article Hibernate, Get Out of My POJO! 休眠,摆脱我的POJO! may be helpful. 可能会有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 LazyInitializationException与Hibernate和Spring MVC - LazyInitializationException with hibernate and spring mvc Spring 数据 JPA Hibernate - LazyInitializationException - Spring Data JPA Hibernate - LazyInitializationException Spring 4 Hibernate 4 JPA LazyInitializationException:无法延迟初始化角色集合 - Spring 4 hibernate 4 JPA LazyInitializationException: failed to lazily initialize a collection of role Spring JPA - org.hibernate.LazyInitializationException:无法初始化代理 - 无 Z71AB3B3AE294B3ABDE46 - Spring JPA - org.hibernate.LazyInitializationException: could not initialize proxy - no Session Spring JPA和LazyInitializationException - Spring JPA and LazyInitializationException 带有 JPA 实体的 Spring 中的 LazyInitializationException - LazyInitializationException in Spring with JPA entity 使用ModelMapper的LazyInitializationException(Spring + Hibernate) - LazyInitializationException with ModelMapper (Spring + Hibernate) @Transactional 上的 Spring Hibernate LazyInitializationException - Spring Hibernate LazyInitializationException on @Transactional 如何修复休眠 JPA 中的 LazyInitializationException - How to fix LazyInitializationException in hibernate JPA 将实体转换为dto时,Spring MVC,休眠LazyInitializationException - Spring mvc, hibernate LazyInitializationException when converting entity to dto
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM