在我的项目中使用 OpenEntityManagerInViewFilter 吗?

发布于 2025-01-06 00:02:26 字数 2369 浏览 1 评论 0原文

由于休眠的延迟加载问题,我尝试设置 springs OpenEntityManagerInViewFilter。

但我无法让它与我已经运行的应用程序一起使用。 为了使用 Open EM,除了向 web.xml 添加内容和创建 applicationContext.xml 之外,我还需要做什么?

谢谢,

我已经添加到 web.xml:

    <filter>
        <filter-name>
            OpenEntityManagerInViewFilter
        </filter-name>
            <filter-class>
                org.springframework.orm.jpa.support.OpenEntityManagerInViewFilter
            </filter-class>
            <init-param>
                <param-name>singleSession</param-name>
                <param-value>true</param-value>
            </init-param>
            <init-param>
                <param-name>flushMode</param-name>
                <param-value>AUTO</param-value>
            </init-param>
    </filter>
    <!-- Include this if you are using Hibernate -->
    <filter-mapping>
        <filter-name>OpenEntityManagerInViewFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

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

    <listener>
    <listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
  </listener>

和 applicationContext.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:p="http://www.springframework.org/schema/p"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="
    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-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/task http://www.springframework.org/schema/task/spring-task-3.0.xsd">

</beans>

我已经可以部署我的应用程序,但是当我尝试启动它时,它会抛出 ex:

org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'entityManagerFactory' is defined

due to Lazy loading problems with hibernate I try to set up springs OpenEntityManagerInViewFilter.

But I cannot get it to work with my already working app.
What else appart from adding things to web.xml and creating a applicationContext.xml to I have to do, in order to use the Open EM?

Thanks

I have added to web.xml:

    <filter>
        <filter-name>
            OpenEntityManagerInViewFilter
        </filter-name>
            <filter-class>
                org.springframework.orm.jpa.support.OpenEntityManagerInViewFilter
            </filter-class>
            <init-param>
                <param-name>singleSession</param-name>
                <param-value>true</param-value>
            </init-param>
            <init-param>
                <param-name>flushMode</param-name>
                <param-value>AUTO</param-value>
            </init-param>
    </filter>
    <!-- Include this if you are using Hibernate -->
    <filter-mapping>
        <filter-name>OpenEntityManagerInViewFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

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

    <listener>
    <listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
  </listener>

and an applicationContext.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:p="http://www.springframework.org/schema/p"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="
    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-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/task http://www.springframework.org/schema/task/spring-task-3.0.xsd">

</beans>

I already can deploy my app, but when I try to launch it throws ex:

org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'entityManagerFactory' is defined

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

一桥轻雨一伞开 2025-01-13 00:02:26

在 applicationContext.xml 中创建一个entityManagerFactory,如下所示:

<!-- Add JPA support -->
 <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    <property name="loadTimeWeaver">
        <bean class="org.springframework.instrument.classloading.InstrumentationLoadTimeWeaver"/>
    </property>
 </bean>

 <!-- Add Transaction support -->
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
    <property name="entityManagerFactory" ref="entityManagerFactory"/>
</bean>

Create an entityManagerFactory into your applicationContext.xml like this:

<!-- Add JPA support -->
 <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    <property name="loadTimeWeaver">
        <bean class="org.springframework.instrument.classloading.InstrumentationLoadTimeWeaver"/>
    </property>
 </bean>

 <!-- Add Transaction support -->
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
    <property name="entityManagerFactory" ref="entityManagerFactory"/>
</bean>
辞别 2025-01-13 00:02:26

由于错误表明您没有在 spring 配置文件中定义entityManagerFactory。或者它是用其他名称定义的。尝试将此初始化参数添加到 web.xml 中的配置中

<init-param>
    <param-name>entityManagerFactoryBeanName</param-name>
    <param-value>entityManagerFactory</param-value>
</init-param>

As the error suggests you do not have a entityManagerFactory defined in your spring config files. Or it is defined with some other name. Try adding this init param to the config in web.xml

<init-param>
    <param-name>entityManagerFactoryBeanName</param-name>
    <param-value>entityManagerFactory</param-value>
</init-param>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文