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

后端 未结 1 1591
星月不相逢
星月不相逢 2020-12-09 04:50

I would like to take advantage of JPA @Entity annotations not to declare class entities a J2SE persistence.xml file. What I\'d like to avoid :



        
相关标签:
1条回答
  • 2020-12-09 05:19

    -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:

    Unpacked Web Persistence JAR

    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.

    0 讨论(0)
提交回复
热议问题