具有多个数据源和不同包的多个EntityManagerFactoryToScan

时间:2018-01-23 08:07:40

标签: hibernate spring-data-jpa

一直试图为此找到解决方案但无法让任何人工作。

我在entityManagerFac中定义了两个root-context.xml个bean,但未定义persistence.xml

    <bean id="entityManagerFac" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean" autowire="byName">
        <property name="dataSource" ref="dataSource" />
        <property name="persistenceUnitName" value="entityManagerFactory1" />
        <property name="packagesToScan">
            <list>
                <value>my.common</value>
                <value>my.biz</value>
            </list>
        </property>
        <property name="jpaVendorAdapter">
            <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" />
        </property>
        <property name="jpaProperties">
            <props>
<!--                <prop key="hibernate.hbm2ddl.auto">validate</prop> -->
                <prop key="hibernate.dialect">org.hibernate.dialect.DB2Dialect</prop>
<!--                    <prop key="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</prop> -->
                    <prop key="spring.jpa.properties.hibernate.show_sql">true</prop>
                    <prop key="spring.jpa.properties.hibernate.use_sql_comments">true</prop>
                    <prop key="spring.jpa.properties.hibernate.format_sql">false</prop>
            </props>
        </property>
    </bean>

    <bean id="entityManagerFac2"  class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean" autowire="byName">
        <property name="dataSource" ref="ds2" />
        <property name="persistenceUnitName" value="entityManagerFactory2" />
        <property name="packagesToScan">
            <list>
                <value>my.new</value>
            </list>
        </property>
        <property name="jpaVendorAdapter">
            <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" />
        </property>
        <property name="jpaProperties">
            <props>
<!--                <prop key="hibernate.hbm2ddl.auto">validate</prop> -->
                <prop key="hibernate.dialect">org.hibernate.dialect.DB2Dialect</prop>
<!--                    <prop key="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</prop> -->
                    <prop key="spring.jpa.properties.hibernate.show_sql">true</prop>
                    <prop key="spring.jpa.properties.hibernate.use_sql_comments">true</prop>
                    <prop key="spring.jpa.properties.hibernate.format_sql">false</prop>
            </props>
        </property>
    </bean>

在设置中,包my.commonmy.biz需要使用datasource1my.new需要使用datasource2

然而,当我尝试运行它时,我收到错误:

java.lang.IllegalArgumentException: Not a managed type:

其中一个模特。

了解这可能是由于模型没有被persistenceUnitName扫描造成的。无论如何在没有引入persistence.xml的情况下克服这个问题吗?

修改 添加jpa:repositories设置:

<jpa:repositories base-package="my.repository.common" entity-manager-factory-ref="entityManagerFac"/>
    <jpa:repositories base-package="my.repository.biz" entity-manager-factory-ref="entityManagerFac"/>
    <jpa:repositories base-package="my.repository.new" entity-manager-factory-ref="entityManagerFac2"/>

1 个答案:

答案 0 :(得分:0)

发现根本原因:在构建中未正确包含应该使用@Entity注释注释的实体类。

一旦纠正,设置按预期工作。