当前位置: 首页 > 面试题库 >

Spring 3.1 + Hibernate 4.1 JPA,实体管理器工厂已注册两次

金骞尧
2023-03-14
问题内容

我使用带有Hibernate 4.1的Spring Framework
3.1作为JPA提供程序,并且具有完整的功能设置,但是每次启动Web应用程序时,我都会看到以下警告消息:

14:28:12,725  WARN pool-2-thread-12 internal.EntityManagerFactoryRegistry:80 - HHH000436: Entity manager factory name (something) is already registered.  If entity manager will be clustered or passivated, specify a unique value for property 'hibernate.ejb.entitymanager_factory_name'

该应用程序正常运行,但是像这样的警告消息困扰着我,数小时的搜索,调整和试验使我无所适从。我试过更改工厂名称并添加和省略配置块,但无济于事。看来,Spring或Hibernate中的某些东西只是两次初始化了实体管理器工厂。

仅供参考,我正在使用LocalContainerEntityManagerFactoryBean的packagesToScan功能来配置实体管理器,而没有persistence.xml文件

我已经将Spring上下文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:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="
           http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
           http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

  <context:property-placeholder location="classpath:jdbc.properties"/>
  <bean id="dataSource" class="org.apache.tomcat.jdbc.pool.DataSource" destroy-method="close">
    <property name="driverClassName" value="${jdbc.nightsword.driverClassName}"/>
    <property name="url" value="${jdbc.nightsword.url}"/>
    <property name="username" value="${jdbc.nightsword.username}"/>
    <property name="password" value="${jdbc.nightsword.password}"/>
  </bean>

  <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    <property name="jpaVendorAdapter">
      <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"/>
    </property>
    <property name="dataSource" ref="dataSource"/>
    <property name="packagesToScan" value="x.y"/>
  </bean>
</beans>

为了完整起见,这里是hibernate.properties:

hibernate.dialect=org.hibernate.dialect.MySQL5Dialect
hibernate.ejb.entitymanager_factory_name=something

这是org.springframework.orm和org.hibernate的摘录调试级别日志输出。您可以看到如何在14:40:06,911首次注册EntityManagerFactory,然后此后立即出现,LocalContainerEntityManagerFactoryFactoryBean从头开始。嗯

INFO: Deploying web application archive /opt/local/share/java/tomcat7/webapps/nightsword.war
14:40:06,149  INFO pool-2-thread-13 jpa.LocalContainerEntityManagerFactoryBean:264 - Building JPA container EntityManagerFactory for persistence unit 'default'
14:40:06,219 DEBUG pool-2-thread-13 type.BasicTypeRegistry:143 - Adding type registration boolean -> org.hibernate.type.BooleanType@4cb91eff

...

14:40:06,882 DEBUG pool-2-thread-13 internal.SessionFactoryRegistry:62 - Initializing SessionFactoryRegistry : org.hibernate.internal.SessionFactoryRegistry@161bb7fe
14:40:06,882 DEBUG pool-2-thread-13 internal.SessionFactoryRegistry:75 - Registering SessionFactory: a3219dd8-7d59-45ac-9a5a-0d13e38dbb04 (<unnamed>)
14:40:06,882 DEBUG pool-2-thread-13 internal.SessionFactoryRegistry:82 - Not binding SessionFactory to JNDI, no JNDI name configured
14:40:06,882 DEBUG pool-2-thread-13 internal.SessionFactoryImpl:487 - Instantiated session factory
14:40:06,882 DEBUG pool-2-thread-13 internal.SessionFactoryImpl:1119 - Checking 0 named HQL queries
14:40:06,883 DEBUG pool-2-thread-13 internal.SessionFactoryImpl:1142 - Checking 0 named SQL queries
14:40:06,887 DEBUG pool-2-thread-13 internal.StatisticsInitiator:110 - Statistics initialized [enabled=false]
14:40:06,910 DEBUG pool-2-thread-13 internal.EntityManagerFactoryRegistry:56 - Initializing EntityManagerFactoryRegistry : org.hibernate.ejb.internal.EntityManagerFactoryRegistry@75cc9008
14:40:06,911 DEBUG pool-2-thread-13 internal.EntityManagerFactoryRegistry:66 - Registering EntityManagerFactory: something 
14:40:06,967  INFO pool-2-thread-13 jpa.LocalContainerEntityManagerFactoryBean:264 - Building JPA container EntityManagerFactory for persistence unit 'default'
14:40:06,967 DEBUG pool-2-thread-13 type.BasicTypeRegistry:143 - Adding type registration boolean -> org.hibernate.type.BooleanType@4cb91eff

...

14:40:07,128 DEBUG pool-2-thread-13 internal.SessionFactoryRegistry:75 - Registering SessionFactory: 81a9b5a6-83aa-46ee-be68-d642e6fda584 (<unnamed>)
14:40:07,128 DEBUG pool-2-thread-13 internal.SessionFactoryRegistry:82 - Not binding SessionFactory to JNDI, no JNDI name configured
14:40:07,129 DEBUG pool-2-thread-13 internal.SessionFactoryImpl:487 - Instantiated session factory
14:40:07,129 DEBUG pool-2-thread-13 internal.SessionFactoryImpl:1119 - Checking 0 named HQL queries
14:40:07,129 DEBUG pool-2-thread-13 internal.SessionFactoryImpl:1142 - Checking 0 named SQL queries
14:40:07,129 DEBUG pool-2-thread-13 internal.StatisticsInitiator:110 - Statistics initialized [enabled=false]
14:40:07,130 DEBUG pool-2-thread-13 internal.EntityManagerFactoryRegistry:66 - Registering EntityManagerFactory: something 
14:40:07,130  WARN pool-2-thread-13 internal.EntityManagerFactoryRegistry:80 - HHH000436: Entity manager factory name (something) is already registered.  If entity manager will be clustered or passivated, specify a unique value for property 'hibernate.ejb.entitymanager_factory_name'

问题答案:

您如何初始化Spring Application Context?您正在使用Spring MVC吗?

我有时看到Spring MVC
XML配置导入其他应用程序。上下文XML,导致将某些bean实例化两次,因为它们是在应用程序上下文和Web应用程序上下文中声明的。



 类似资料:
  • 问题内容: 一个很长的问题,请忍受我。 我们将Spring + JPA用于Web应用程序。我的团队在讨论如何在注入的(基于泛型的东西对AppFuse中提供的线DAO,我们不使用过的注射某种原因)。我们正在使用“应用程序管理的持久性”。 反对注入a的论点是它太重了,因此不是必需的,这就是我们需要的。而且,由于Spring将为每个Web请求创建一个DAO的新实例(我对此表示怀疑),因此不会有任何并发​

  • 我使用带有Hibernate 4.1的Spring Framework 3.1作为JPA提供程序,并且我有一个功能齐全的设置,但每次启动Web应用程序时,我都会看到这条警告消息: 这个应用程序运行良好,但是像这样的警告信息让我很困扰,几个小时的搜索、调整和试验让我一无所获。我尝试过更改工厂名称,添加和省略大量配置,但都无济于事。似乎Spring或Hibernate中的某些东西只是初始化实体管理器工

  • 编辑:堆栈跟踪如下: 下面是persistence.xml:

  • http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd“> 但面对这个例外: 任何帮助都将不胜感激...

  • 我刚刚开始使用Spring ROO,并使用数据库逆向工程命令生成了我的实体类。然而每当我试图调用生成的实体类中的一个CRUD方法时,我总是得到这个异常:Java . lang . illegalstateexception:实体管理器没有被注入(Spring Aspects JAR是否被配置为AJC/AJDT方面库?) 我怀疑(通过查看生成的文件)EntityManager没有被注入到类中。你能告

  • 我有一个在Eclipse中定义的非常基本的JavaEE应用程序(一个EJB,一个带有PrimeFaces的网页,一个JPA实体)。它有四个项目,一个用于EAR,一个用于EJB,一个用于JPA,一个用于Web部件。问题是,我试图在JPA项目的persistence.xml中配置HiberNate作为持久化框架,但它并没有像我预期的那样完全工作。在我的EJB(@Statless)中,我可以通过两种方式