JPA - 多个罐子定义@Entity对象 [英] JPA - multiple jars defining @Entity objects

查看:263
本文介绍了JPA - 多个罐子定义@Entity对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们正在开发一个非常模块化的方式与Spring,Hibernate和Maven的Web应用程序。有定义的数据访问核心项目和查看具体的东西,然后有定义的逻辑和实体( @Entity )的模块,然后有Web应用程序定义控制器和视图。

We are developing a web application with Spring, Hibernate and Maven in a very modular fashion. There are core projects defining data access and view specific stuff, then there are modules defining logic and entities (@Entity) and then there's the web app defining controller and view.

现在我们已经定义安全实体,如帐号和角色的安全模块,我们有一个原型模块定义一些例如实体,如客户和订单。两者有一个 PersistenceUnit 中定义的的persistence.xml 这是pretty除了PersistenceUnit名称多为空,因为所有的数据库配置是在与datasource.xml Web应用程序完成的。 Web应用程序应该加载两个罐子作为Maven的依赖关系。

Now we have a security module defining security entities like account and role and we have a prototype module defining some example entities like customer and order. Both have a PersistenceUnit defined inside a persistence.xml which is pretty much empty except for the PersistenceUnit name, as all database configuration is done in the web app with a datasource.xml. The web app is supposed to load both jars as maven dependencies.

这两个项目将建设精品,autocscanning所有实体和它们各自的单元测试创​​建它们。他们还将得到成功加载web应用程序里面,如果单独添加。

Both projects will build fine, autocscanning all entities and creating them for their respective unit tests. They will also get loaded inside the web app successfully if added individually.

然而,只要两者在同一时间加载,第二个将覆盖 PersistenceUnit 第一个,并因此创建一个抛出:IllegalArgumentException:不是一个用于从第一个所有实体的实体。如果这两个项目具有不同的持久性单元,装载web应用程序将引发另一个异常说没有单一的默认持久性单元定义。

However, as soon as both are loaded at the same time, the second one will override the PersistenceUnit of the first one and thus create an IllegalArgumentException : Not an entity for all entities from the first one. If both projects have a different persistence unit, loading the web app will throw another exception saying that no single default persistence unit defined.

所以..我怎么能得到所有 @Entity 注释类在我的web应用程序加载,而不必定义它们所有的里面的持久性的.xml (如<一个href=\"http://javathoughts.capesugarbird.com/2009/02/jpa-and-multiple-persistence-units.html\">here)而是通过组件扫描?
<一href=\"http://forum.springsource.org/showthread.php?61763-Problems-with-JPA-and-multiple-persistence-units\">This似乎是一个想法,虽然我不知道如何使用和测试它...

So.. how can I get all @Entity annotated classes to load up in my web app without having to define them all inside the persistence.xml (like here) but rather via component scan? This seems like an idea, though I don't know how to use and test it...

我想,我们要么合并Web应用程序内的所有PersistenceUnits或编程方式加载所有实体。定义他们的persistence.xml内硬coded不为我们的选择。

I think we either have to merge all PersistenceUnits inside the web app or load all Entities programmatically. Defining them hard-coded inside the persistence.xml is not an option for us.

推荐答案

我们使用这类似于模块布局,但我们已经放在持久化上下文中进行应用程序的战争的一部分,注入的EntityManager到模块中的DAO的。除了单元测试模块没有PU。我们这样做是因为我们担心,该交易跨越多个模块可能会造成麻烦。

We use a simliar module layout, but we have placed the persistence context in the war-part of out application and inject the entitymanager into the DAOs of the modules. Except for unittesting the modules don't have a PU. We did this because we were afraid, that transaction spanning multiple modules could cause trouble.

在DAO

@PersistenceContext
private EntityManager em;

在persistance.xml你需要争取所​​有实体的元素。

In the persistance.xml you need to enlist all entities with the elements.

<persistence-unit name="myPU">
    <class>com.Entity1</class>      
<class>com.Entity2</class>

等。

这篇关于JPA - 多个罐子定义@Entity对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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