是否可以使用JPA和Hibernate扫描jar文件中的实体 [英] Is it possible to scan Entities in jar files using JPA and hibernate
问题描述
我有以下情况:
- 项目A-包含一些实体并且是独立的
- 项目B-包含一些实体并且是独立的
- 项目C-包含很少的实体,并且依赖于项目A&项目B.
我正在使用Maven来管理依赖关系和构建.
I am using Maven to manage dependencies and builds.
当我尝试测试项目A和项目B时,一切顺利.它们每个都有一个persistence.xml和一个单独的持久上下文.
When I try to test Project A and project B it goes through fine. Each of them has a persistence.xml and a separate persistent context.
当我运行Project C时,它会映射任何实体.我尝试使用自动检测功能,并指定了jar文件属性...但是似乎没有任何作用.
When I run Project C , It does map any of the entities. I have tried to use the auto-detect, specified the jar file attribute ... but nothing seems to work.
它给了我一个映射异常,说未知实体,不会持久存在,也不会从项目A或B中读取实体.我在这里发布了3个persistence.xml文件.
It gives me a Mapping Exception saying unknown entity and wont persist or read the Entities from Projects A or B. I have posted the 3 persistence.xml files here.
此外,我尝试使用class属性并使用相同的持久性上下文,但是它只是找不到文件.
Also, I tried using the class attribute and using the same persistent context but it just wont find the files.
我们非常感谢您的帮助.
Any help is really appreciated.
提前谢谢!
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence
http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd" version="1.0">
<persistence-unit name="A" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.Oracle9Dialect"/>
<property name="hibernate.connection.driver_class" value="oracle.jdbc.OracleDriver"/>
<property name="hibernate.show_sql" value="true"/>
<property name="hibernate.connection.username" value="username"/>
<property name="hibernate.connection.password" value="password"/>
<property name="hibernate.connection.url" value="jdbc:oracle:thin:@webdev.epi.web:1521/webdev.world"/>
<property name="hibernate.max_fetch_depth" value="3"/>
<property name="hibernate.archive.autodetection" value="class"/>
</properties>
</persistence-unit>
</persistence>
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence
http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd" version="1.0">
<persistence-unit name="B" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.Oracle9Dialect"/>
<property name="hibernate.connection.driver_class" value="oracle.jdbc.OracleDriver"/>
<property name="hibernate.show_sql" value="true"/>
<property name="hibernate.connection.username" value="username"/>
<property name="hibernate.connection.password" value="password"/>
<property name="hibernate.connection.url" value="jdbc:oracle:thin:@webdev.epi.web:1521/webdev.world"/>
<property name="hibernate.max_fetch_depth" value="3"/>
<property name="hibernate.archive.autodetection" value="class"/>
</properties>
</persistence-unit>
</persistence>
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence
http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd" version="1.0">
<persistence-unit name="C" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<jar-file>A-0.0.1-SNAPSHOT.jar</jar-file>
<jar-file>B-0.0.1-SNAPSHOT.jar</jar-file>
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.Oracle9Dialect"/>
<property name="hibernate.connection.driver_class" value="oracle.jdbc.OracleDriver"/>
<property name="hibernate.show_sql" value="true"/>
<property name="hibernate.connection.username" value="username"/>
<property name="hibernate.connection.password" value="password"/>
<property name="hibernate.connection.url" value="jdbc:oracle:thin:@webdev.epi.web:1521/webdev.world"/>
<property name="hibernate.max_fetch_depth" value="3"/>
<property name="hibernate.archive.autodetection" value="class"/>
</properties>
</persistence-unit>
</persistence>
推荐答案
实际上,我找到了一种使其工作的方法. 在专案A和B,我自动检测到我的实体,在Project C中,我明确列出了映射的类,这些实体现在已被映射,并且可以正常工作.
Actually, I found a way to make it work. In Projects A & B , I autodetect my entities and in Project C , I explicitly list the mapped classes, the entities are now getting mapped and it works.
希望它对其他人有帮助!
Hope it helps some one else !
这篇关于是否可以使用JPA和Hibernate扫描jar文件中的实体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!