gpt4 book ai didi

java - 如何配置 OpenEntityManagerInViewFilter Spring MVC

转载 作者:行者123 更新时间:2023-11-30 07:53:39 24 4
gpt4 key购买 nike

我想使用OpenEntityManagerInViewFilter来避免延迟初始化加载。这是我的 web.xml 配置:

<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
WEB-INF/mvc-dispatcher-servlet.xml
</param-value>

</context-param>

<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<filter>
<filter-name>hibernateFilter</filter-name>
<filter-class>org.springframework.orm.jpa.support.OpenEntityManagerInViewFilter</filter-class>
</filter>

<filter-mapping>
<filter-name>hibernateFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

我仍然收到异常(exception):

Servlet.service() for servlet [mvc-dispatcher] 在路径 [/...] 的上下文中抛出异常

Request processing failed; nested exception is 
org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: ..., could not initialize proxy - no Session] with root cause
org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: ...., could not initialize proxy - no Session

有人知道我做错了什么吗?

最佳答案

结果是因为我像这样配置了 contextConfigLocation:

<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
WEB-INF/mvc-dispatcher-servlet.xml
</param-value>

调度程序 servlet 与 applicationContext 不同,它被加载了两次或其他什么,所以解决方案是
1.创建applicationContext.xml,2.然后将entitymanager bean从mvc调度程序移动到applicationContext.xml。3. 我的 web.xml 现在看起来像这样:

<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<filter>
<filter-name>oemInViewFilter</filter-name>
<filter-class>org.springframework.orm.jpa.support.OpenEntityManagerInViewFilter</filter-class>
<init-param>
<param-name>entityManagerFactoryBeanName</param-name>
<param-value>entityManagerFactory</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>oemInViewFilter</filter-name>
<url-pattern>/*</url-pattern>
<dispatcher>REQUEST</dispatcher>
</filter-mapping>

关于java - 如何配置 OpenEntityManagerInViewFilter Spring MVC,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32988394/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com