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

查看:14
本文介绍了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 应用程序应该将两个 jars 作为 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 带注释的类加载到我的网络应用程序中,而不必在 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 应用程序内的所有 PersistenceUnit,要么以编程方式加载所有实体.在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天全站免登陆