我将 Spring 与 JPA 和 Hibernate 一起使用。我有一些用@Repositoy 注释的DAO 类和一些控制器类。当我在控制器中调用 dao 的一个方法来加载一些实体时,我会取回实体,然后,我想获取一些其他实体,这些实体存储在第一个加载实体的字段中。但是那个时候spring已经关闭了session,不能再懒加载了。
我的 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>
Dao中的方法注解如下:
@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: failed to lazily initialize a collection of role:...,no session or session was closed
根本原因:org.hibernate.LazyInitializationException:未能延迟初始化角色集合:...,没有会话或会话已关闭
我试图在 Controller wirt @Transactional 中注释该方法,并在 db.xml 的注释驱动属性中设置 mode="aspectj",但没有任何效果。有没有办法懒加载用户的订单?
对于任何帮助,谢谢你的期待!