Spring 3.1 + Hibernate 4.1 JPA,Entity manager factory注册了两次 [英] Spring 3.1 + Hibernate 4.1 JPA, Entity manager factory is registered twice

查看:118
本文介绍了Spring 3.1 + Hibernate 4.1 JPA,Entity manager factory注册了两次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将Spring Framework 3.1与Hibernate 4.1一起用作JPA提供程序,并且我有一个功能齐全的设置,但每次启动Web应用程序时,都会看到以下警告消息:

  14:28:12,725 WARN pool-2-thread-12 internal.EntityManagerFactoryRegistry:80  -  HHH000436:实体管理器工厂名称(东西)已经注册。如果实体管理器将被聚集或钝化,为属性'hibernate.ejb.entitymanager_factory_name'指定一个唯一值

应用程序的功能很好,但是像这样的警告消息让我感到困扰,而搜索,调整和试验的时间却让我无处可寻。我试过改变工厂名称并添加和省略配置块,都无济于事。看起来Spring或Hibernate中的某些东西只是初始化实体管理器工厂两次。

仅供参考,我使用LocalContainerEntityManagerFactoryBean的packagesToScan功能来配置实体管理器,而无需一个persistence.xml文件。



我将春天的上下文XML压缩到了下面,问题依然存在:

 <?xml version =1.0encoding =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 =dataSourceclass =org.apache.tomcat.jdbc.pool.DataSourcedestroy-method =close>
< property name =driverClassNamevalue =$ {jdbc.nightsword.driverClassName}/>
< property name =urlvalue =$ {jdbc.nightsword.url}/>
< property name =usernamevalue =$ {jdbc.nightsword.username}/>
< property name =passwordvalue =$ {jdbc.nightsword.password}/>
< / bean>

< bean id =entityManagerFactoryclass =org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean>
< property name =jpaVendorAdapter>
< bean class =org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter/>
< / property>
< property name =dataSourceref =dataSource/>
< property name =packagesToScanvalue =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的,并且此后LocalContainerEntityManagerFactoryBean看起来从头开始全部开始。

 信息:部署Web应用程序归档文件/opt/local/share/java/tomcat7/webapps/nightsword.war 
14:40:06,149 INFO pool-2-thread-13 jpa.LocalContainerEntityManagerFactoryBean:264 - 为持久化单元'默认'构建JPA容器EntityManagerFactory
14:40:06,219 DEBUG pool-2-thread-13类型。 BasicTypeRegistry:143 - 添加类型注册boolean - > org.hibernate.type.BooleanType@4cb91eff



14:40:06,882 DEBUG pool-2-thread-13 internal.SessionFactoryRegistry:62 - 初始化SessionFactoryRegistry: org.hibernate.internal.SessionFactoryRegistry@161bb7fe
14:40:06,882 DEBUG pool-2-thread-13 internal.SessionFactoryRegistry:75 - 注册SessionFactory:a3219dd8-7d59-45ac-9a5a-0d13e38dbb04(<未命名>)
14:40:06,882 DEBUG pool-2-thread-13 internal.SessionFactoryRegistry:82 - 不绑定SessionFactory到JNDI,没有配置JNDI名称
14:40:06,882 DEBUG pool-2-thread-13 internal.SessionFactoryImpl:487 - 实例化的会话工厂
14:40:06,882 DEBUG pool-2-thread-13 internal.SessionFactoryImpl:1119 - 检查0个已命名的HQL查询
14:40:06,883调试池-2 -thread-13 internal.SessionFactoryImpl:1142 - 检查0个命名SQL查询
14:40:06,887 DEBUG pool-2-thread-13 internal.StatisticsInitiator:110 - 统计初始化[enabled = false]
14 :40:06,910 DEBUG pool-2-thread-13 internal.EntityManagerFactoryRegistry:56 - 初始化EntityManagerFactoryRegistry:org.hibernate.ejb.internal.EntityManagerFactoryRegistry@75cc9008
14:40:06,911 DEBUG pool-2-thread-13 internal.EntityManagerFactoryRegistry:66 - 注册EntityManagerFactory:something
14:40:06,967 INFO pool-2-thread-13 jpa.LocalContainerEntityManagerFactoryBean:264 - 构建JPA容器EntityManagerFactory for persistence unit'default'
14:40:06,967 DEBUG pool-2-thread-13 type.BasicTypeRegistry :143 - 添加类型注册boolean - > org.hibernate.type.BooleanType@4cb91eff



14:40:07,128 DEBUG pool-2-thread-13 internal.SessionFactoryRegistry:75 - 注册SessionFactory: 81a9b5a6-83aa-46ee-be68-d642e6fda584(< unnamed>)
14:40:07,128 DEBUG pool-2-thread-13 internal.SessionFactoryRegistry:82 - 没有将SessionFactory绑定到JNDI,没有配置JNDI名称
14:40:07,129 DEBUG pool-2-thread-13 internal.SessionFactoryImpl:487 - 实例化会话工厂
14:40:07,129 DEBUG pool-2-thread-13 internal.SessionFactoryImpl:1119 - Checking 0 named HQL查询
14:40:07,129 DEBUG pool-2-thread-13 internal.SessionFactoryImpl:1142 - 检查0个已命名的SQL查询
14:40:07,129 DEBUG pool-2-thread-13 internal.StatisticsInitiator :110 - 统计初始化[enabled = false]
14:40:07,130 DEBUG pool-2-thread-13 internal.EntityManagerFactoryRegistry:66 - 注册EntityManagerFactory:something
14:40:07,130 WARN pool-2 -thread-13 internal.EntityManagerFactoryRegistry:80 - HHH000436:实体管理器工厂名称(东西)已经注册。如果实体管理器将被聚集或钝化,为属性'hibernate.ejb.entitymanager_factory_name'指定一个唯一值'


解决方案

你如何初始化你的Spring应用程序上下文?您使用的是Spring MVC吗?



有时我会看到Spring MVC XML配置导入其他应用程序。上下文XML,因此在应用程序上下文和Web应用程序上下文中声明了两次实例化了一些bean。


I'm using Spring Framework 3.1 with Hibernate 4.1 as a JPA provider, and I have a fully functional setup, but every time the web app is started I see this warning message:

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'

The application functions just fine, but warning messages like that bother me, and hours of searching and tweaking and experimenting have lead me nowhere. I've tried changing the factory name and adding and omitting chunks of configuration, all to no avail. It appears that something in Spring or Hibernate is just initializing the entity manager factory twice.

FYI, I'm using the packagesToScan functionality of the LocalContainerEntityManagerFactoryBean to configure the entity manager without a persistence.xml file.

I've pared down my spring context XML to the following and the problem persists:

<?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>

For completeness, here's hibernate.properties:

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

And here's excerpted debug-level log output from both org.springframework.orm and org.hibernate. You can see how at 14:40:06,911 the EntityManagerFactory is registered from the first time, and immediately thereafter the LocalContainerEntityManagerFactoryBean appears to start all over from the beginning. Huh.

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'

解决方案

How are you initializing your Spring Application Context? Are you using Spring MVC?

I've seen sometimes Spring MVC XML configurations importing the other app. context XML, causing instancing twice some beans because they are declared in the application context and the web application context.

这篇关于Spring 3.1 + Hibernate 4.1 JPA,Entity manager factory注册了两次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆