JPA 2.0: Adding entity classes to PersistenceUnit *from different jar* automatically

前端 未结 8 573
余生分开走
余生分开走 2020-11-29 02:23

I have a maven-built CDI-based Java SE app, which has a core module, and other modules.
Core has the persistence.xml and some entities. Modules hav

8条回答
  •  情歌与酒
    2020-11-29 03:06

    I have a slightly different setup where I am placing persistence.xml in the WAR file but some of its dependencies includes @Entity annotated classed to include in the persistence unit.

    I have solved my problem using Maven a bit like Adrian Shum described in #3, but using the element to include the jars to be scanned for @Entity annotations.

    I added a property to my-web/pom.xml for each dependency including extra entities. All my jars are part of a Maven multiproject build so for me it looks like.

    
        common-${project.version}.jar
        foo-${project.version}.jar
    
    

    I thereafter add the following to the persistence.xml

    
    
        
            java:jboss/datasources/mysource
    
            lib/${common.jar}
            lib/${foo.jar}
    
            ...
        
    
    

    Lastly I configure the maven-resource-plugin in web/pom.xml to replace the $expressions in persistence.xml with the properties set in the POM

    
      
        
          src/main/resources
          true
          
            **/persistence.xml
          
        
        
          src/main/resources
          false
          
            **/persistence.xml
          
        
      
      ...
    
    

提交回复
热议问题