JPA 2.0:自动将实体类添加到 PersistenceUnit *来自不同的 jar* [英] JPA 2.0: Adding entity classes to PersistenceUnit *from different jar* automatically

查看:51
本文介绍了JPA 2.0:自动将实体类添加到 PersistenceUnit *来自不同的 jar*的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 maven 构建的基于 CDI 的 Java SE 应用程序,它有一个 核心 模块和其他模块.
Core 有 persistence.xml 和一些实体.模块有额外的实体.

I have a maven-built CDI-based Java SE app, which has a core module, and other modules.
Core has the persistence.xml and some entities. Modules have additional entities.

如何将实体添加到持久化单元的聚光灯下?

How can I add the entities to the spotlight of the persistence unit?

我已阅读 Hibernate 手册,http://docs.jboss.org/hibernate/stable/entitymanager/reference/en/html/configuration.html#setup-configuration-packaging

I have read Hibernate manual, http://docs.jboss.org/hibernate/stable/entitymanager/reference/en/html/configuration.html#setup-configuration-packaging

我也看到了这些 SO 问题

I have also seen these SO questions

我正在寻找一种解决方案,让 Hibernate 扫描所有加载的类,或者从其他 jar 文件中获取一些配置文件(例如 CDI 对 beans.xml 所做的).

I am looking for a solution where Hibernate would scan for all loaded classes, or, would pick up some config file form the other jars (like e.g. CDI does with beans.xml).

我的应用程序不使用 Spring.我不坚持可移植性 - 我会坚持使用 Hibernate.

My app does not use Spring. I don't insist on portability - I'll stick with Hibernate.

  • 有这样的解决方案吗?
  • 有没有办法从 persistence.xml 创建 PU 并以编程方式向其中添加类?
  • 是否可以在创建后将@Entity 类添加到 EntityManagerFactory 中?
  • Is there some such solution?
  • Is there's a way to create a PU from persistence.xml and add classes to it programmatically?
  • Can I add @Entity classes to EntityManagerFactory after it was created?

更新:我在org. hibernate. ejb. Ejb3Configuration中发现:

public Ejb3Configuration configure(String persistenceUnitName, Map integration)  

http://docs.jboss.org/hibernate/entitymanager/3.6/javadocs/

推荐答案

解决方法有几种:

  1. 我需要中所述吗?元素在persistence.xml?,你可以设置hibernate.archive.autodetection 属性,Hibernate 应该能够从类路径中查找所有带注释的类.但是,这不符合 JPA 规范.

  1. As described in Do I need <class> elements in persistence.xml?, you can set hibernate.archive.autodetection property and Hibernate should be able to look up all annotated classes from classpath. However, that's not JPA spec compliant.

如果您使用的是 Spring,从 Spring 3.1.2(甚至可能更早一点)开始,在 LocalContainerEntityManagerFactoryBean 中,您可以定义 packageToScan 这将询问LocalContainerEntityManagerFactoryBean 在类路径中扫描以查找所有带注释的类.同样,不符合 JPA 规范.

If you are using Spring, from Spring 3.1.2 (or probably even a bit earlier), in LocalContainerEntityManagerFactoryBean, you can define packageToScan which will ask LocalContainerEntityManagerFactoryBean to scan in classpath to find all annotated classes. Again, not JPA spec compliant.

我使用 Maven 作为构建工具.多年前,我写了一个小插件,它会在构建过程中生成persistence.xml.该插件将扫描构建类路径以找出所有带注释的类,并将它们列在生成的persistence.xml 中.这是最乏味的,但结果是符合 JPA 规范的.一个缺点(我相信这不适用于大多数人)是查找发生在构建时,而不是运行时.这意味着如果您的应用程序只在部署/运行时提供实体 JAR,而不在构建时提供实体 JAR,那么这种方法将不起作用.

I was using Maven as build tools. Years before, I have written a little plugin which will generate persistence.xml during build process. The plugin will scan from build classpath to find out all annotated classes, and list them in the generated persistence.xml. This is most tedious but the result is JPA spec compliant. One drawback (which does not apply to most people I believe) is the lookup happens in build-time, not runtime. That means if you are having an application for which entities JARs are provided only at deploy/runtime but not build time, this approach is not going to work.

这篇关于JPA 2.0:自动将实体类添加到 PersistenceUnit *来自不同的 jar*的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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