JPA - multiple jars defining @Entity objects

后端 未结 5 1356
暖寄归人
暖寄归人 2020-12-03 07:31

We are developing a web application with Spring, Hibernate and Maven in a very modular fashion. There are core projects defining data access and view specific stuff, then th

相关标签:
5条回答
  • 2020-12-03 07:39

    We use a similar module layout, but we place the persistence context in the war-part of our application and inject the entitymanager into the DAOs of the modules. Except for unit testing, the modules don't have a PU. We did this because we were afraid, that a transaction spanning multiple modules could cause trouble.

    In the DAO

    @PersistenceContext
    private EntityManager em;
    

    In the persistance.xml you need to enlist all entities with the elements.

    <persistence-unit name="myPU">
        <class>com.Entity1</class>      
    <class>com.Entity2</class>
    

    etc.

    0 讨论(0)
  • 2020-12-03 07:48

    Well, we' re having a similar problem on our development cycle. If your referencing entities are in another -ejb.jar file (that's our case) you can bind these entities with

    <jar-file>relativePathToYourJar/dependent-entities-ejb.jar</jar-file>

    tag. Notice that you should also include a persistence.xml under dependent-entities-ejb.jar's META-INF folder. Further information could be found here.

    0 讨论(0)
  • 2020-12-03 07:52

    Since the question seems to still get attention, I'll post our latest solution to the problem.

    We are now auto-scanning all packages instead of using persistence.xml files anywhere.

    In our datasource.xml we added the line

    <property name="packagesToScan" value="our.basepackage" />
    

    Almost complete datasource.xml:

    <!-- Bean definition here -->
    
    <bean id="ourDataSource"
        class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="com.mysql.jdbc.Driver" />
                <!-- define connection properties -->       
    </bean>
    
    
    <bean id="entityManagerFactory"
        class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
        <property name="dataSource" ref="ourDataSource" />
        <property name="packagesToScan" value="our.basepackage" />
        <property name="jpaVendorAdapter">
            <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
                <property name="showSql" value="false" />
                <property name="generateDdl" value="true" />
                <property name="databasePlatform" value="org.hibernate.dialect.MySQL5InnoDBDialect" />
            </bean>
        </property>
    
    </bean>
    
    <tx:annotation-driven transaction-manager="transactionManager" />
    <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager" />
    

    0 讨论(0)
  • 2020-12-03 07:57

    I've made a solution like tugcem, but because I am using Maven I would hate to have the jar version numbers in my persistence.xml. The solution I came up with is described here: JPA 2.0: Adding entity classes to PersistenceUnit *from different jar* automatically

    0 讨论(0)
  • 2020-12-03 08:00

    The classes will be in the class loader.

    This so answer Scanning Java annotations at runtime.

    Is that what you want? To scan for @Entity annotated and add them to a PersistenceUnit?

    Regards.

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