从 jar 自动检测 Hibernate 中的类 [英] Autodetect classes in Hibernate from jar
问题描述
我有一个包含多个实体的 JavaEE 项目,其中包含以下 persistence.xml
文件:
I have a JavaEE project with several entities with the following persistence.xml
file:
...
<properties>
<!-- Scan for annotated classes -->
<property name="hibernate.archive.autodetection" value="class"/>
...
它似乎有效.然后将该项目作为 JAR 部署到两个不同的项目.其中一个persistence.xml
文件与上面的基本相同,即使用了Hibernate的autodetection
特性.但是,它似乎不起作用(因为我认为它需要加载搜索 jar 中的实体).当我尝试手动列出两个 xml 文件中的所有实体时,一切正常.
And it seems to work. The project is then deployed as a JAR to two different projects. In one them, the persistence.xml
file is basically the same as the one above, i.e. uses the autodetection
feature of Hibernate. However, it does not seem to work (because I think it needs to load search for the entities in a jar). When I tried manually listing all the entities in both xml files, everything works correctly.
它适用于 jar-file,但仅适用于 jar 的绝对路径.对于团队项目来说毫无用处.
It works with jar-file but only with the absolute path to the jar. Pretty useless for a team project.
推荐答案
如果我没看错的话,在它不起作用的情况下,实体位于与持久性不同的 JAR 中.使用的 xml,对吗?
If I read you correctly, in the case where it does not work, the entities are in a different JAR than the persistence.xml that's used, right?
我认为你是对的,这就是问题所在.您需要使用 jar-file 元素告诉 Hibernate 要扫描哪些 JAR.请参阅 Hibernate 文档中的解释和示例 这里
I think you're correct that's the problem. You need to tell Hibernate which JAR(s) to scan using the jar-file element. See the explanation and examples in the Hibernate docs here
这篇关于从 jar 自动检测 Hibernate 中的类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!