Java 从 jar 中自动检测 Hibernate 中的类

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18012532/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 21:47:22  来源:igfitidea点击:

Autodetect classes in Hibernate from jar

javahibernatejakarta-eejboss

提问by

I have a JavaEE project with several entities with the following persistence.xmlfile:

我有一个包含多个实体的 JavaEE 项目,其中包含以下persistence.xml文件:

...
<properties>
    <!-- Scan for annotated classes -->
    <property name="hibernate.archive.autodetection" value="class"/>
...

And it seems to work. The project is then deployed as a JAR to two different projects. In one them, the persistence.xmlfile is basically the same as the one above, i.e. uses the autodetectionfeature 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 部署到两个不同的项目。其中之一,persistence.xml文件与上面的基本相同,即使用了autodetectionHibernate的特性。但是,它似乎不起作用(因为我认为它需要加载搜索 jar 中的实体)。当我尝试手动列出两个 xml 文件中的所有实体时,一切正常。

Edit:It works with jar-filebut only with the absolute path to the jar. Pretty useless for a team project.

编辑:它适用于jar 文件,但仅适用于jar的绝对路径。对于团队项目来说非常无用。

采纳答案by Jeroen K

If I read you correctly, in the case where it does not work, the entities are in a differentJAR than the persistence.xml that's used, right?

如果我没看错的话,在它不起作用的情况下,实体位于使用的 persistence.xml不同的JAR 中,对吗?

I think you're correct that's the problem. You need to tell Hibernate which JAR(s) to scan using the jar-fileelement. See the explanation and examples in the Hibernate docs here

我认为你是对的,这就是问题所在。您需要使用jar-file元素告诉 Hibernate 要扫描哪些 JAR 。在此处查看Hibernate 文档中的解释和示例

回答by Martin Vysny

I think Hibernate only scans for JPA Entities inside jar files, and not in classes/ folders for example, or it only scans in the jar with the persistence.xml, or something like that. Instead of trying to bend the Hibernate scanner, I felt it would be easier to scan for entities myself. It is a lot easier than I anticipated.

我认为 Hibernate 只扫描 jar 文件中的 JPA 实体,而不是在类/文件夹中,或者它只扫描带有 persistence.xml 或类似文件的 jar。与其试图弯曲 Hibernate 扫描仪,我觉得自己扫描实体会更容易。这比我预期的要容易得多。

 val entities = mutableListOf<Class<*>>()
AnnotationDetector(object : AnnotationDetector.TypeReporter {
    override fun reportTypeAnnotation(annotation: Class<out Annotation>?, className: String?) {
        entities.add(Class.forName(className))
    }

    override fun annotations(): Array<out Class<out Annotation>> = arrayOf(Entity::class.java)
}).detect("com.github")

VaadinOnKotlin.entityManagerFactory = Persistence.createEntityManagerFactory("sample", mapOf(AvailableSettings.LOADED_CLASSES to entities))

I have used the detector which is built-in in the atmosphere jar file; but you can use perhaps https://github.com/rmuller/infomas-aslor others as stated here: Scanning Java annotations at runtimeThe full code example is located here: https://github.com/mvysny/vaadin-on-kotlin/blob/master/vok-example-crud/src/test/java/com/github/vok/example/crud/Server.kt

我使用了大气 jar 文件中内置的检测器;但您可以使用https://github.com/rmuller/infomas-asl或其他,如下所述:在运行时扫描 Java 注释完整的代码示例位于此处:https: //github.com/mvysny/vaadin-on -kotlin/blob/master/vok-example-crud/src/test/java/com/github/vok/example/crud/Server.kt

None of the following helped: adding

以下都没有帮助:添加

<exclude-unlisted-classes>false</exclude-unlisted-classes>

to the persistence.xmlfile did nothing (according to doc it is ignored in JavaSE anyway); adding

persistence.xml文件什么也没做(根据 doc 它在 JavaSE 中无论如何都会被忽略);添加

<property name="hibernate.archive.autodetection" value="class, hbm" />

to the persistence.xmlfile did nothing (I'm not using hbm anyways). Don't waste your time trying to add those, they won't enable proper auto-scan.

persistence.xml文件什么也没做(反正我没有使用 hbm)。不要浪费时间尝试添加这些,它们不会启用正确的自动扫描。

回答by Andreas Dietrich

Using the empty element <jar-file></jar-file>works e.g. with WildFly 8.2.1.Final (Hibernate 4.3.7.Final) :) as mentioned here. (<property name="hibernate.archive.autodetection" value="class, hbm" />did not work)

使用空元素<jar-file></jar-file>适用于例如 WildFly 8.2.1.Final (Hibernate 4.3.7.Final) :) 如这里提到的。(<property name="hibernate.archive.autodetection" value="class, hbm" />没有工作)

(but it did not work outside of WildFly for Eclipse-run JUnit tests where we had to remove it. Which is not a problem since the libraries are already in the classpath. The error was something like: java.net.URISyntaxException: Expected scheme-specific part at index 5: file:> => <java.lang.IllegalArgumentException: Unable to visit JAR file:. Cause: Expected scheme-specific part at index 5: file:> => <javax.persistence.PersistenceException: Unable to build entity manager factory)

(但它没有WildFly的工作之外,我们不得不将其删除Eclipse的运行JUnit测试,因为库已经在classpath这是没有问题的错误是这样的:。 java.net.URISyntaxException: Expected scheme-specific part at index 5: file:> => <java.lang.IllegalArgumentException: Unable to visit JAR file:. Cause: Expected scheme-specific part at index 5: file:> => <javax.persistence.PersistenceException: Unable to build entity manager factory