JPA 2.0:自动从不同的jar *中将实体类添加到PersistenceUnit * [英] JPA 2.0: Adding entity classes to PersistenceUnit *from different jar* automatically
问题描述
我有一个maven构建的基于CDI的Java SE应用程序,它有一个核心模块和其他模块。
Core有持久性.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
- 如何合并/扩展不同JAR中的持久化单元?
- 定义persistence.xml之外的jpa实体类
- < a href =http://stackoverflow.com/questions/2838634/programmatically-loading-entity-classes-with-jpa-2-0>使用JPA 2.0以编程方式加载实体类?
- How can I merge / extend persistence units from different JARs?
- define jpa entity classes outside of persistence.xml
- Programmatically loading Entity classes with JPA 2.0?
我正在寻找一个解决方案,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并以编程方式向其添加类?
- 在创建之后,我可以向
EntityManagerFactory
添加@Entity类吗? li>
- 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/
推荐答案
有几种解决方法:
There are several way to solve it:
-
如我需要< class> persistence.xml中的元素?,可以设置
hibernate.archive.autodetection
属性,Hibernate应该能够从类路径中查找所有注释的类。
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,这种方法将不起作用。
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:自动从不同的jar *中将实体类添加到PersistenceUnit *的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!