多个entityManagerFactory,用于2个不同的数据源

时间:2016-05-18 20:52:08

标签: spring hibernate jpa spring-data-jpa

我想为2个不同的数据库保存2个数据源(两者都是mysql),如下所示:

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

     <bean id="dataSource-A"
    class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="driverClassName" value="${jdbc.driverClassName}" />
    <property name="url" value="${jdbc.url-A}" />
    <property name="username" value="${jdbc.username}" />
    <property name="password" value="${jdbc.password}" />
</bean>

<bean id="entityManagerFactory-A"
    class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    <property name="dataSource" ref="dataSource-A" />
    <property name="jpaVendorAdapter" ref="jpaVendorAdapter" />
    <!-- spring based scanning for entity classes -->
    <property name="packagesToScan" value="com.package-A" />
</bean>

    <bean id="dataSource-B"
    class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="driverClassName" value="${jdbc.driverClassName}" />
    <property name="url" value="${jdbc.url-B}" />
    <property name="username" value="${jdbc.username}" />
    <property name="password" value="${jdbc.password}" />
</bean>

<bean id="entityManagerFactory-B"
    class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    <property name="dataSource" ref="dataSource-B" />
    <property name="jpaVendorAdapter" ref="jpaVendorAdapter" />
    <!-- spring based scanning for entity classes -->
    <property name="packagesToScan" value="com.package-B" />
</bean>

现在当我这么做时,beanInitializer会给出找不到entityManagerFactory的错误。

我如何拥有多个entityManagerFactory,用于多个数据库以及xml中最好的方法。

我搜索了很多,但没有一个能解决我的问题。

如果我只定义一个entityManagerFactory,它会很好用,我只是坚持在相同的应用程序上下文中有多个entityManagerFactory。

1 个答案:

答案 0 :(得分:1)

下面是为不同类型的数据库保留多个持久性单元的示例。同样,可以为两个sql数据库保留2个单元。

<?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="oraPersistent" transaction-type="RESOURCE_LOCAL">
        <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
        <class>com.kulhade.us.ora.entity.BilltoAddress</class>
        <exclude-unlisted-classes>true</exclude-unlisted-classes>
        <shared-cache-mode>ENABLE_SELECTIVE</shared-cache-mode>
        <validation-mode>AUTO</validation-mode>
        <properties>
            <property name="javax.persistence.jdbc.driver" value="${db.driver}"/>
            <property name="javax.persistence.jdbc.url" value="${db.url}"/>
            <property name="javax.persistence.jdbc.user" value="${db.username}"/>
            <property name="javax.persistence.jdbc.password" value="${db.password}"/>
            <property name="hibernate.cache.use_query_cache" value="true"/>
            <property name="hibernate.cache.use_second_level_cache" value="true"/>
            <property name="hibernate.generate_statistics" value="true"/>
            <property name="hibernate.cache.region.factory_class" value="org.hibernate.cache.ehcache.SingletonEhCacheRegionFactory"/>
            <property name="hibernate.jdbc.batch_size" value="50"/>
        </properties>
    </persistence-unit>
    <persistence-unit name="mongoPersistent" transaction-type="JTA">
        <!-- Use Hibernate OGM provider: configuration will be transparent -->
        <provider>org.hibernate.ogm.jpa.HibernateOgmPersistence</provider>
        <class>com.kulhade.us.mongo.entity.Sample</class>
        <class>com.kulhade.us.mongo.entity.SampleLine</class>
        <exclude-unlisted-classes>true</exclude-unlisted-classes>
        <properties>
            <property name="hibernate.ogm.datastore.provider" value="mongodb" />
            <property name="hibernate.ogm.datastore.database" value="${mongodb.name}"/>
            <property name="hibernate.ogm.mongodb.host" value="${mongodb.host}"/>
            <property name="hibernate.ogm.datastore.port" value="${mongodb.port}"/>
            <!--<property name="hibernate.ogm.datastore.document.association_storage" value="ASSOCIATION_DOCUMENT"/>
            <property name="hibernate.ogm.mongodb.association_document_storage" value="COLLECTION_PER_ASSOCIATION"/>-->
            <property name="hibernate.transaction.jta.platform" value="org.hibernate.service.jta.platform.internal.JBossStandAloneJtaPlatform"/>
        </properties>
    </persistence-unit>
</persistence>

这些多余的持久性单元可以在Spring orm中使用。以下是它的例子。

<bean id="pum" class="org.springframework.orm.jpa.persistenceunit.DefaultPersistenceUnitManager">
    <property name="persistenceXmlLocations">
        <list>
            <value>org/springframework/orm/jpa/domain/persistence-multi.xml</value>
            <value>classpath:/my/package/**/custom-persistence.xml</value>
            <value>classpath*:META-INF/persistence.xml</value>
        </list>
    </property>
    <property name="dataSources">
        <map>
            <entry key="localDataSource" value-ref="local-db"/>
            <entry key="remoteDataSource" value-ref="remote-db"/>
        </map>
    </property>
    <!-- if no datasource is specified, use this one -->
    <property name="defaultDataSource" ref="remoteDataSource"/>
</bean>

<bean id="emf" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    <property name="persistenceUnitManager" ref="pum"/>
    <property name="persistenceUnitName" value="myCustomUnit"/>
</bean>