Spring文件中的JPA Autoscan实体

时间:2015-02-06 23:10:18

标签: java spring maven jpa

我有两个项目: 普通和人力资源

项目common是一个父项目(打包为Jar),hrm是一个依赖项目(打包为war) 依赖性通过maven指定

Maven依赖关系指定为:

<dependency>
    <groupId>com.talentera</groupId>
    <artifactId>common</artifactId>
    <version>0.0.1-SNAPSHOT</version>
</dependency>

我在hrm项目中的持久性XML:

<?xml version="1.0" encoding="UTF-8"?>
<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="talentera" transaction-type="RESOURCE_LOCAL">
        <description>example of enabling the second level cache.</description>
        <jta-data-source>java:jboss/datasources/TalenteraDS</jta-data-source>   
        <exclude-unlisted-classes>false</exclude-unlisted-classes>
        <jar-file>../../lib/common-0.0.1-SNAPSHOT.jar</jar-file>
        <properties>
            <property name="hibernate.cache.use_second_level_cache" value="false" />
            <property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5InnoDBDialect" />
            <property name="hibernate.bytecode.use_reflection_optimizer" value="false" />
            <property name="hibernate.show_sql" value="true" />
            <property name="hibernate.use_outer_join" value="false" />
            <property name="hibernate.cache.use_structured_entries" value="true" />
            <property name="hibernate.generate_statistics" value="true" />
            <property name="hibernate.id.new_generator_mappings" value="false" />
            <property name="hibernate.default_batch_fetch_size" value="500" />
            <property name="hibernate.max_fetch_depth" value="5" />
            <property name="hibernate.jdbc.batch_size" value="1000" />
            <property name="jboss.as.jpa.managed" value="false" />

            <property name="hibernate.archive.autodetection" value="class, hbm"/>
        </properties>
    </persistence-unit>
</persistence>

Spring配置:

<bean id="entityManagerFactory"
    class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    <property name="dataSource" ref="appDataSource" />
    <property name="persistenceUnitName" value="talentera" />

    <property name="jpaVendorAdapter">
       <bean
           class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
           <property name="database" value="MYSQL" />
           <property name="showSql" value="true" />                
       </bean>
    </property>
    <property name="jpaDialect">
        <bean class="org.springframework.orm.jpa.vendor.HibernateJpaDialect" />
    </property>
</bean>

<bean id="jpaTransactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
    <property name="entityManagerFactory" ref="entityManagerFactory" />
    <property name="dataSource" ref="appDataSource" />
</bean>

请帮助我使实体可用于hrm项目中的spring-jpa自动识别

common打包为Jar,hrm打包为战争。

如果我需要在此处提供更多信息,请与我们联系。

1 个答案:

答案 0 :(得分:1)

我对复合单元有同样的问题,我用这种方式解决了:

你必须改变

<jar-file>../../lib/common-0.0.1-SNAPSHOT.jar</jar-file>

<jar-file>WEB-INF/lib/common-0.0.1-SNAPSHOT.jar</jar-file>

ii应该工作