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

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

问题描述

我们正在以非常模块化的方式使用 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.

现在我们有一个定义安全实体(如帐户和角色)的安全模块,我们有一个原型模块定义了一些示例实体(如客户和订单).两者都在 persistence.xml 中定义了一个 PersistenceUnit,除了 PersistenceUnit 名称外,它几乎是空的,因为所有数据库配置都是在 web 应用程序中使用 datasource.xml 完成的.Web 应用程序应该将这两个 jar 作为 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.

两个项目都将构建良好,自动扫描所有实体并为各自的单元测试创​​建它们.如果单独添加,它们也将成功加载到 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 : Not an entity对于第一个实体中的所有实体.如果两个项目具有不同的持久性单元,则加载 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 应用程序中,而不必在 persistence.xml 中定义它们(例如 此处),而是通过组件扫描?似乎是一个想法,虽然我不知道如何使用和测试它...

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 中硬编码定义它们对我们来说不是一个选择.

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.

推荐答案

我们使用了类似的模块布局,但是我们将持久化上下文放在应用程序的战争部分,并将实体管理器注入模块的 DAO 中.除了单元测试,模块没有 PU.我们这样做是因为我们害怕,跨多个模块的事务可能会导致麻烦.

We use a similar module layout, but we place the persistence context in the war-part of our application and inject the entitymanager into the DAOs of the modules. Except for unit testing, the modules don't have a PU. We did this because we were afraid, that a 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 对象的多个 jar的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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