如何在使用JPA和Hibernate时解决LazyInitializationException [英] How to solve the LazyInitializationException when using JPA and Hibernate

查看:506
本文介绍了如何在使用JPA和Hibernate时解决LazyInitializationException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为一个想要使用延迟初始化的客户开发一个项目。
当使用默认的延迟加载模式映射类时,它们总是会得到延迟初始化异常。

  @JoinTable(name =join_profilo_funzional,joinColumns = {@JoinColumn(name =profilo_id,referencedColumnName =profilo_id)},inverseJoinColumns = {@JoinColumn(name =funzionalita_id,referencedColumnName =funzionalita_id)})
/ /@ManyToMany(fetch=FetchType.EAGER) - 如果未注释,则不会出现异常
@ManyToMany
私人收藏< Funzionalita> funzionalitaIdCollection;

有没有使用JPA类的标准模式来避免这种错误?



欢迎使用片段,非常感谢您的时间。

解决方案

Hibernate 4.1.6终于解决了这个问题:https://hibernate.atlassian.net/browse/HHH-7457

您需要设置hibernate-property hibernate。 enable_lazy_load_no_trans = true

以下是在Spring中执行的操作:

  < bean id =entityManagerFactory
class =org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean>
< property name =dataSourceref =myDataSource/>
< property name =packagesToScanvalue =com.mycompany.somepackage/>
< property name =jpaVendorAdapterref =hibernateVendorAdapter/>
< property name =jpaDialectref =jpaDialect/>
< property name =jpaProperties>
<道具>
< prop key =hibernate.enable_lazy_load_no_trans> true< / prop>
< /道具>
< / property>
< / bean>

瞧;现在,您不必担心LazyInitializationException,而在hibernate会话之外导航您的领域模型(JPA发言中的持久性上下文)。


I am working on a project for a customer who wants to use lazy initialization. They always get "lazy initialization exception" when mapping classes with the default lazy loading mode.

@JoinTable(name = "join_profilo_funzionalita", joinColumns = {@JoinColumn(name =    "profilo_id", referencedColumnName = "profilo_id")}, inverseJoinColumns = {@JoinColumn(name = "funzionalita_id", referencedColumnName = "funzionalita_id")})
//@ManyToMany(fetch=FetchType.EAGER) - no exceptions if uncommented
@ManyToMany 
private Collection<Funzionalita> funzionalitaIdCollection;

Is there a standard pattern using JPA classes to avoid this error?

Snippets are welcome, thanks a lot for your time.

解决方案

Hibernate 4.1.6 finally solves this issue: https://hibernate.atlassian.net/browse/HHH-7457

You need to set the hibernate-property hibernate.enable_lazy_load_no_trans=true

Here's how to do it in Spring:

<bean id="entityManagerFactory"
      class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    <property name="dataSource" ref="myDataSource"/>
    <property name="packagesToScan" value="com.mycompany.somepackage"/>
    <property name="jpaVendorAdapter" ref="hibernateVendorAdapter"/>
    <property name="jpaDialect" ref="jpaDialect"/>
    <property name="jpaProperties">
        <props>
            <prop key="hibernate.enable_lazy_load_no_trans">true</prop>
        </props>
    </property>
</bean>

Voila; Now you don't have to worry about LazyInitializationException while navigating your domain-model outside of a hibernate-session (persistence-context in "JPA-speak")

这篇关于如何在使用JPA和Hibernate时解决LazyInitializationException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆