Java 尽管有Spring事务,LazyInitializationException还是异常?

Java 尽管有Spring事务,LazyInitializationException还是异常?,java,hibernate,spring,jpa,Java,Hibernate,Spring,Jpa,下面的方法导致抛出org.hibernate.LazyInitializationException,如果您能帮助我理解原因,我将不胜感激。我正在使用jpa2/Hibernate&Spring JPA2/Hibernate正在使用默认的事务持久性上下文,因此,下面的方法不应该允许延迟加载吗 @Service public class GalleryService { @Transactional(readOnly=true) public Response getGallery(

下面的方法导致抛出
org.hibernate.LazyInitializationException
,如果您能帮助我理解原因,我将不胜感激。我正在使用jpa2/Hibernate&Spring

JPA2/Hibernate正在使用默认的
事务
持久性上下文,因此,下面的方法不应该允许延迟加载吗

@Service
public class GalleryService {
    @Transactional(readOnly=true)
    public Response getGallery(@PathParam("id") int id) {
        Gallery g = daoWrapper.findById(Gallery.class, id);
        ...
        GalleryDto gDto = new GalleryDto();
        ...
        // getImages() returns a collection of 'image' objects.
        gDto.setImages(g.getImages());
        return Response.ok(gDto).build();
    }
}
注意
daoWrapper
是一个方便的类,用于包装实体管理器方法

@Repository
public class daoWrapper implements BaseDao {

   @PersistenceContext(unitName="persistStore") 
   private EntityManager em;

   @Override
   public <T,U> T findById(Class<T> entity, U id) {
        return this.em.find(entity, id);
   }
   ...
}
@存储库
公共类DAO包装器实现BaseDao{
@PersistenceContext(unitName=“persistStore”)
私人实体管理者;
@凌驾
公共T findById(类实体,U id){
返回此.em.find(实体,id);
}
...
}
应用程序上下文文件:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
   xmlns:context="http://www.springframework.org/schema/context"
   xmlns:aop="http://www.springframework.org/schema/aop"
   xmlns:tx="http://www.springframework.org/schema/tx"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation="http://www.springframework.org/schema/beans
   http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
   http://www.springframework.org/schema/context
   http://www.springframework.org/schema/context/spring-context-3.0.xsd
   http://www.springframework.org/schema/aop
   http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
   http://www.springframework.org/schema/tx
   http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">

<context:property-placeholder location="classpath:database.properties" />
<context:annotation-config />
<context:component-scan base-package="com.myapp.services"/>
<tx:annotation-driven/>
...
</beans>

...

我们需要更多的信息来诊断,但我倾向于看到您的签名,因为它位于一个未被代理的控制器类中。@Transactional只在bean工厂代理的类上工作,在许多设置上下文的常用方法中,这不包括控制器类。

@Affe要求的是您的spring配置?如果您得到一个LazyInitializationException,那么就没有事务。正如@Affe所提到的,只有在创建代理时才会启动事务,这可能是因为您的配置中存在一些问题

我接到电话时也发现了同样的问题

session.clear();
介于两者之间

entity = session.get(...);


因此,只需检查您的代码,使其不使用此方法。

定义您要注释的bean的spring配置,您正在使用的代理类型,以及daoWrapper的实际功能。是的,看起来您正在使用JDK代理?GalleryService需要实现一个具有该方法的接口,以便spring能够生成代理并应用事务性建议。有没有一种方法可以解决这个问题,而不必使用所需的方法创建和实现接口?我还有其他类似构造的类,它们可以毫无问题地延迟加载。有什么想法吗?当然,有很多,尝试一下,看看它是否能解决问题,它会告诉我们代理确实是个问题。它还与调用方法的JDK代理有关。如果在同一个类上调用另一个方法,事务性方法将不起作用。
entity.getLazyCollection();