使用MergingPersistenceUnitManager加载实体 [英] using the MergingPersistenceUnitManager to load entity

查看:193
本文介绍了使用MergingPersistenceUnitManager加载实体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个项目设置,其中有一个存在于父War文件的WEB-INF / lib文件夹中的模块。这个模块包含一个persistence.xml和一个实体,我需要在启动时加载JPA容器。不知怎的,我需要合并战争的持久性单位和lib jar。我的战争的persistence.xml存在于WEB-INF / classes / META-INF中,因此它将把WEB-INF / classes作为持久性根,并不理解我的lib jar中的实体。我发现困难的方式。



我偶然发现了多个人为这个问题提出了解决方案

http://ancientprogramming.blogspot.com/2007/05/multiple-persistencexml-files-and.html a>
我还发现Spring的Data-jpa项目有一个MergingPersistenceUnitManager,它将合并实体的类定义。



这是我的配置

 < bean id =pum class =org.springframework.data.jpa.support.MergingPersistenceUnitManager> 
< property name =persistenceXmlLocations>
< list>
< value> classpath *:META-INF / persistence.xml< / value>
< / list>
< / property>
< property name =defaultDataSourceref =dataSource>< / property>
< / bean>


< bean id =entityManagerFactoryclass =org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean>
< property name =dataSourceref =dataSource/>
< property name =persistenceUnitNamevalue =LineManagement/>
< property name =persistenceUnitManagerref =pum>< / property>
< property name =jpaVendorAdapter>
< bean class =org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter>
< property name =generateDdlvalue =false/>
< property name =showSqlvalue =false/>
< property name =databasePlatformref =cpsHibernateDialectClassName/>
< / bean>
< / property>

不起作用。它给我一个错误
java.lang.NoSuchMethodError:org.springframework.data.jpa.support.MergingPersistenceUnitManager.getPersistenceUnitInfo(Ljava / lang / String;)Lorg / springframework / orm / jpa / persistenceunit / MutablePersistenceUnitInfo;



我不知道它是如何给我这个错误的。根据我的理解,MergingPersistenceUnitManager扩展了DefaultPersistenceManager。唯一我怀疑的是可能会有冲突。



这是我的依赖关系。 spring-orm-3.0.2-RELEASE.jar和spring-data-jpa-1.0.3-RELEASE.jar。

我可以回到古代编程解决方案,但不应该它只是开箱即用?

解决方案

您有不匹配的版本的spring-orm和spring-data-jpa 。一般来说,任意版本不能保证一起玩。



MutablePersistenceUnitInfo getPersistenceUnitInfo - 在版本orm版本3.0.2中,MergingPersistenceUnitManager(实际上来自其超类DefaultPersistenceUnitManager)缺少方法。

从版本3.0.5中的同一个类可以找到这个方法: DefaultPersistenceUnitManager.java maven pom for spring-data-jpa-1.0.3 lists依赖于spring-orm 3.0.5。所以你的问题可以通过使用sprin-orm 3.0.5版来解决。

I have a project setup where i have a module which is present in the WEB-INF/lib folder of the parent war file. This module contains a persistence.xml and an entity which i need to be loaded when the JPA container loads during startup. Somehow i need to merge the persistence units of the war and the lib jar. the persistence.xml of my war is present in WEB-INF/classes/META-INF hence it would take WEB-INF/classes as the persistence root and wouldnt understand the entity from my lib jar. That i found out the hard way.

I stumbled across multiple people suggesting solutions to this problem

http://ancientprogramming.blogspot.com/2007/05/multiple-persistencexml-files-and.html And i also found out that there is Spring's Data-jpa project which has a MergingPersistenceUnitManager which will merge the class definitions of the entities.

Here is my configuration

<bean id="pum" class="org.springframework.data.jpa.support.MergingPersistenceUnitManager">
  <property name="persistenceXmlLocations">
    <list>
     <value>classpath*:META-INF/persistence.xml</value>
    </list> 
  </property>
  <property name="defaultDataSource" ref="dataSource"></property>
</bean>


<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    <property name="dataSource" ref="dataSource" />
    <property name="persistenceUnitName" value="LineManagement" />
    <property name="persistenceUnitManager" ref="pum"></property>
    <property name="jpaVendorAdapter">
        <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
            <property name="generateDdl" value="false" />
            <property name="showSql" value="false" />
            <property name="databasePlatform" ref="cpsHibernateDialectClassName" />
        </bean>
    </property>

which doesnt work. It gives me an error java.lang.NoSuchMethodError:org.springframework.data.jpa.support.MergingPersistenceUnitManager.getPersistenceUnitInfo(Ljava/lang/String;)Lorg/springframework/orm/jpa/persistenceunit/MutablePersistenceUnitInfo;

I have no clue how it gives me this error. From my understanding the MergingPersistenceUnitManager extends the DefaultPersistenceManager. Only thing i suspect is that there might be a conflict.

Here are my dependencies. spring-orm-3.0.2-RELEASE.jar and spring-data-jpa-1.0.3-RELEASE.jar.

I can go back to the ancient programming solution, but shouldnt it just work out of the box?

解决方案

You have mismatching versions of spring-orm and spring-data-jpa. In general arbitrary versions are not guaranteed to play well together.

MutablePersistenceUnitInfo getPersistenceUnitInfo - method is absent from the MergingPersistenceUnitManager (or actually from it's superclass DefaultPersistenceUnitManager) in version orm version 3.0.2.

From same class in version 3.0.5 you can find this method: DefaultPersistenceUnitManager.java Also maven pom for spring-data-jpa-1.0.3 lists dependency to spring-orm 3.0.5. So your problem is solved by using sprin-orm version 3.0.5.

这篇关于使用MergingPersistenceUnitManager加载实体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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