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

前端 未结 8 530
余生分开走
余生分开走 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 02:45

    I have a similar problem and solved it with Hibernate's Integrator SPI:

    @Override
    public void integrate(Configuration configuration,
        SessionFactoryImplementor sessionFactory,
        SessionFactoryServiceRegistry serviceRegistry) {
    
        configuration.addAnnotatedClass(MyEntity.class);
        configuration.buildMappings();
    }
    

    The Integrator is provided as Java service.

    0 讨论(0)
  • 2020-11-29 02:51

    You can do use this concept: https://wiki.eclipse.org/Packaging_and_Deploying_EclipseLink_JPA_Applications_(ELUG)

    <persistence version="2.1" xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
       <persistence-unit name="mamcaPU" transaction-type="JTA">
            <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
            <jta-data-source>mamcaPU</jta-data-source>
            <mapping-file>/META-INF/common-layer-mappings.xml</mapping-file>
        </persistence-unit>
    </persistence>
    

    common-layer-mappings.xml

    <entity-mappings>   
       <entity class="vub.be.mamca.entity.Surveyactorgrouptable"></entity>
       <entity class="vub.be.mamca.entity.Userevaluationelicitationtable"></entity>
       <entity class="vub.be.mamca.entity.Userevaluationtable"></entity>
       <entity class="vub.be.mamca.entity.Usertable"></entity>
       <entity class="vub.be.mamca.entity.Userweightelicitationtable"></entity>
    </entity-mappings>
    
    0 讨论(0)
  • 2020-11-29 02:58

    There are several way to solve it:

    1. As described in Do I need <class> elements in persistence.xml?, you can set hibernate.archive.autodetection property and Hibernate should be able to look up all annotated classes from classpath. However, that's not JPA spec compliant.

    2. If you are using Spring, from Spring 3.1.2 (or probably even a bit earlier), in LocalContainerEntityManagerFactoryBean, you can define packageToScan which will ask LocalContainerEntityManagerFactoryBean to scan in classpath to find all annotated classes. Again, not JPA spec compliant.

    3. I was using Maven as build tools. Years before, I have written a little plugin which will generate persistence.xml during build process. The plugin will scan from build classpath to find out all annotated classes, and list them in the generated persistence.xml. This is most tedious but the result is JPA spec compliant. One drawback (which does not apply to most people I believe) is the lookup happens in build-time, not runtime. That means if you are having an application for which entities JARs are provided only at deploy/runtime but not build time, this approach is not going to work.

    0 讨论(0)
  • 2020-11-29 03:01

    Ejb3Configuration has been removed in 4.3.0. If you don't want to create a Hibernate's Integrator, you can use the property hibernate.ejb.loaded.classes.

    properties.put(org.hibernate.jpa.AvailableSettings.LOADED_CLASSES, entities);
    Persistence.createEntityManagerFactory("persistence-unit", properties);
    

    Where entities is a List<Class> of entity classes.

    0 讨论(0)
  • 2020-11-29 03:04

    for JPA 2+ this does the trick

    <jar-file></jar-file>
    

    scan all jars in war for annotated @Entity classes

    0 讨论(0)
  • 2020-11-29 03:05

    I've experienced the same issue and unfortunately there is no easy solution, it basically looks like JPA wasn't designed to be used this way. One of solutions is to have just one persistence.xml per top-level project (application). It's kind of similar to log4j configuration. The persistence.xml has to list all classes (using <class>) or, if it's not Java SE app, jar files (using <jar-file>) that are used by the application. This way you can put entities from multiple modules (jars) into single persistence unit. The drawback is obvious: you have to list everything in one file.

    EDIT: I have (possibly) found another solution that uses XML mapping files, check it out here: Multiple jars, single persistence unit solution?

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