微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

尽管有Spring事务,LazyInitializationException吗?

下面的方法导致抛出org.hibernate.LazyInitializationException,感谢您对理解原因的帮助.我正在使用JPA 2 /休眠&弹簧.

JPA 2 / 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);
   }
   ...
}

应用程序上下文文件

<?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工厂代理的类上起作用,以许多常见的方式来设置上下文,这不包括控制器类.

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐