繁体   English   中英

用jpa和hibernate在春季延迟加载实体

[英]lazy loading entities in spring with jpa and hibernate

我在JPA和Hibernate中使用Spring。 我有一些用@Repositoy注释的DAO类和一些控制器类。 当我在控制器中调用dao的方法之一以加载某些实体时,我取回了Entity,然后,我想获取一些存储在第一个加载的实体的字段中的其他实体。 但是那时spring已经关闭了会话,并且不再可能进行延迟加载。

我的db.xml配置:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-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/tx http://www.springframework.org/schema/tx/spring-tx.xsd
        http://www.springframework.org/schema/data/jpa
        http://www.springframework.org/schema/data/jpa/spring-jpa.xsd" default-autowire="byName">

    <!-- Scans within the base package of the application for @Components to configure as beans -->
    <bean id="placeholderConfig"
        class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="location" value="classpath:db.properties" />
    </bean>
    <tx:annotation-driven transaction-manager="transactionManager"/>
    <!-- <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
    <property name="entityManagerFactory" ref="jpaVendorAdapter" />
    </bean> -->
    <bean id="entityManagerFactory"
        class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
        <property name="jpaVendorAdapter">
            <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
                <property name="showSql" value="true" />
                <property name="generateDdl" value="true" />
                <property name="databasePlatform" value="${db.dialect}" />
            </bean>
        </property>     
    </bean>

    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
        destroy-method="close">
        <property name="driverClassName" value="${db.driver}" />
        <property name="url" value="${db.url}" />
        <property name="username" value="${db.username}" />
        <property name="password" value="${db.password}" />
    </bean>

    <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager" />
</beans>

道中的方法注释如下:

@Transactional(readOnly = true, propagation=Propagation.REQUIRED)

现在我想做这样的事情:

@Controller
public class HomeController{

    @Autowired
    private UserDao userDao;

    @RequestMapping(value = "/", method = RequestMethod.GET)
    public ResponseEntity<String> home(){
        ...
        User user = userDao.findUser(id);
        Set<Order> orders = user.getOrders();
        ...
    String myResult = ...;
    return jsonService.generateResponse(myResult);
    }

}

@Repository
public class UserDao{

    @PersistenceContext
    private EntityManager entityManager;

    public User findUser(Integer id){
        return entityManager.find(User.class, id);
    }
}

订单集应延迟加载,但出现以下异常:org.springframework.web.util.NestedServletException:请求处理失败; 嵌套的异常是org.hibernate.LazyInitializationException:无法延迟初始化角色的集合:...,没有会话或会话被关闭

根本原因:org.hibernate.LazyInitializationException:无法延迟初始化角色集合:...,没有会话或会话被关闭

我试图对Controller wirt @Transactional中的方法进行注释,并且还在db.xml中的注释驱动属性中设置mode =“ aspectj”,但没有任何效果。 有什么办法可以延迟加载用户的订单?

对于任何帮助,感谢您的期待!

您可以使用特殊过滤器在Web视图中获取会话。 添加到您的web.xml

<filter>
    <filter-name>jpaFilter</filter-name>
    <filter-class>org.springframework.orm.jpa.support.OpenEntityManagerInViewFilter</filter-class>
</filter>
<filter-mapping>
    <filter-name>jpaFilter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

过滤器的工作原理如下:

  • 过滤器拦截servlet请求
  • 过滤器打开一个EntityManager并将其绑定到当前线程
  • Web控制器称为
  • Web控制器呼叫服务
  • 事务拦截器开始一个新的事务,检索线程绑定的EntityManager并将其绑定到该事务
  • 调用服务,使用EntityManager做一些事情,然后返回
  • 事务拦截器刷新EntityManager,然后提交事务
  • Web控制器准备视图,然后返回
  • 视图已建立
  • 筛选器关闭EntityManager并将其与当前线程解除绑定

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM