Is there a way to scan JPA entities not to declare persistent classes in a persistence.xml file?

时光总嘲笑我的痴心妄想 提交于 2019-11-28 07:38:23
Cody

-Make sure your entities and persistence.xml end up in the same classpath when you build your jar.

The entities cannot be scanned if they are sitting in another classpath. If you need to have them sitting in different classpaths, heres one trick I've seen to make it work: No autodetection of JPA Entities in maven-verify.

If you are unsure where things are ending, you can unzip the .jar file and peak. This is an unpacked persistence web project:

Notice my classes are down the com directory, and my persistence.xml is in the META-INF directory. Both are in the same 'classes' classpath, so autoscan will work.

-Set hibernate.archive.autodetection property.

<!-- Scan for annotated classes and Hibernate mapping XML files -->
<property name="hibernate.archive.autodetection" value="class, hbm" />

-Add false to the persistence-unit

<persistence-unit name=...>
<exclude-unlisted-classes>false</exclude-unlisted-classes>
    ...

Hopefully one of those will work for you.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!