2015-11-21 44 views
0

我使用spring 4与hibernate 4和JPA进行了数据库连接。当我尝试获得一个懒惰的初始化字段时,我得到了一个LazyInitializationException。Spring 4 hibernate 4 JPA LazyInitializationException:未能延迟初始化角色集合

我的applicationContext.xml:

<context:annotation-config /> 
<context:component-scan base-package="xy" /> 

<jpa:repositories base-package="xy.repositories" /> 

<bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" /> 
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"> 
    <property name="persistenceUnitName" value="punit" /> 
    <property name="dataSource" ref="dataSource" /> 

    <property name="jpaVendorAdapter"> 
     <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"> 
      <property name="showSql" value="true" /> 
     </bean> 
    </property> 
    <property name="jpaPropertyMap"> 
     <map> 
      <entry key="hibernate.dialect" value="org.hibernate.dialect.MySQL5InnoDBDialect" /> 
      <entry key="hibernate.hbm2ddl.auto" value="update" /> 
      <entry key="hibernate.format_sql" value="true" /> 
     </map> 
    </property> 
</bean> 

<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager"> 
    <property name="entityManagerFactory" ref="entityManagerFactory" /> 

</bean> 


<tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true" /> 

<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> 
    <property name="driverClassName" value="com.mysql.jdbc.Driver" /> 
    <property name="url" value="jdbc:mysql://localhost:3306/keysystem" /> 
    <property name="username" value="username" /> 
    <property name="password" value="password" /> 
</bean> 

<bean class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor"/> 

型号:

@Entity 
@Table 
public class Employee { 
    @Id 
    @GeneratedValue 
    private long employeeId; 
    private String firstName; 
    private String lastName; 
    @OneToMany(fetch = FetchType.LAZY, mappedBy = "employee") 
    @Cascade(CascadeType.ALL) 
    @Fetch(FetchMode.SELECT) 
    private Set<Legitimacy> legitimacies; 
    ... 

我发现只有MVC过滤解决方案,但我不使用MVC。我试过Hibernate.initialize(...)和@Transational,@Fetch注解,但这些都没有解决我的问题。

这里是堆栈跟踪:当您尝试访问原始会话外的引用对象发生

Caused by: org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: xy.Employee.legitimacies, could not initialize proxy - no Session 
    at org.hibernate.collection.internal.AbstractPersistentCollection.throwLazyInitializationException(AbstractPersistentCollection.java:575) 
    at org.hibernate.collection.internal.AbstractPersistentCollection.withTemporarySessionIfNeeded(AbstractPersistentCollection.java:214) 
    at org.hibernate.collection.internal.AbstractPersistentCollection.initialize(AbstractPersistentCollection.java:554) 
    at org.hibernate.collection.internal.AbstractPersistentCollection.read(AbstractPersistentCollection.java:142) 
    at org.hibernate.collection.internal.PersistentSet.iterator(PersistentSet.java:180) 
    ... 64 more 
+0

http://stackoverflow.com/questions/33318404/jpa-how-to-return-null-instead-of-lazyinitializationexception/33325493#33325493 接受的答案解释了它为什么会发生以及如何解决它。如果您尝试访问'legitimacies'的内容,那么在事务尚未完全初始化的情况下设置事务外,您将无法获得此异常。 –

+0

@Transactional注释对我不起作用,我不想使用Eager初始化,因为它的性能。 – Faraine

+0

然后我认为你应该再提供两个代码部分,以便我们可以确定确切的问题以及如何解决它。请添加您用于获取Employee对象的代码,以及您尝试访问合法性设置内容的代码。你可以做的是在休眠会话仍然打开时初始化Set。 –

回答

0

懒例外。

基本上,你这样做是:

* Open a session 
* Fetch an object, let's say X 
* Close the session 
* Try to access X.getY() 

一种解决方案是在同一时间获取X和Y,而会议是开放的。

1

Guillaume是对的,您正在代码中的某个位置使用由Hibernate管理的代理,该代理位于事务性上下文之外。

因此,当您调用尚未初始化的getLegitimacies()时,Hibernate尝试通过抓取会话并进行数据库调用来填充其代理。

如果您在控制器中,例如没有会话可用,则会引发异常。您可以(通过使用在例如你的HQL查询FETCH关键字)来获取你的legitimacies设定在你的DAO一些点:

FROM Employee e LEFT JOIN FETCH e.legitimacies WHERE <your_predicate>; 

你也应该没有操纵你的实体,如果不在服务/ DAO层,但使用DTO来避免这些问题。

相关问题