加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

尽管有Spring事务,LazyInitializationException吗?

发布时间:2020-12-15 01:19:08 所属栏目:大数据 来源:网络整理
导读:下面的方法导致抛出org.hibernate.LazyInitializationException,感谢您对理解原因的帮助.我正在使用JPA 2 /休眠弹簧. JPA 2 / Hibernate使用默认的事务持久性上下文,因此,下面的方法是否不应该允许延迟加载? @Servicepublic class GalleryService { @Transa

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读