我正在使用带JPA和Hibernate的Spring。我有一些使用@Repositoy和一些Controller-Classes注释的DAO类。当我在我的控制器中调用其中一个dao的方法来加载一些实体时,我会返回实体,之后,我想得到一些存储在第一个加载实体的字段中的其他实体。但那时春天已经关闭了会话,懒惰的加载已不再可能。
我的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:懒得初始化角色集合:...,没有会话或会话被关闭
根本原因:org.hibernate.LazyInitializationException:懒得初始化角色集合:...,没有关闭会话或会话
我尝试在Controller wirt @Transactional中注释该方法,并在db.xml中的注释驱动属性中设置mode =“aspectj”,但没有任何效果。有没有办法懒得加载用户的订单?
任何帮助,感谢您的期待!
答案 0 :(得分:5)
您可以使用特殊过滤器在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>
过滤器的工作原理如下: