...
Spring MVC + JPA开发中遇到对象延迟加载,session关闭的情况解决方法如下:
xml 代码
-
<bean name="openEntityManagerInViewInterceptor"
-
class="org.springframework.orm.jpa.support.OpenEntityManagerInViewInterceptor">
-
<property name="entityManagerFactory">
-
<ref bean="entityManagerFactory" />
-
property>
- bean>
-
- <bean id="viewResolver"
-
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
-
<property name="prefix" value="/WEB-INF/jsp/" />
-
<property name="suffix" value=".jsp" />
- bean>
-
- <bean id="simpleUrlHandlerMapping"
-
class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
-
<property name="interceptors">
-
<list>
-
<ref bean="openEntityManagerInViewInterceptor" />
-
list>
-
property>
-
<property name="mappings">
-
<props>
-
<prop key="hello.html">helloControllerprop>
-
<prop key="login.html">loginActionprop>
-
<prop key="userManager.html">userControllerprop>
-
<prop key="userList.html">userListControllerprop>
-
props>
-
property>
- bean>
这种方法和hibernate的OpenSessionInViewInterceptor是一样的