小编典典

Spring JPA Hibernate,具有更多数据源

hibernate

我必须在Hibernate,Jpa的应用程序(弹簧)中使用两个不同的数据库。我想直接将不同的表定义为不同的数据源。所以我使用两个不同的持久性单元,然后尝试使用

<property name="packagesToScan" value="it.two.app.domain.first" />

<property name="packagesToScan" value="it.two.app.domain.second" />

将不同的表放入不同的包中。但这不起作用。用第一个数据源来影响所有表。然后我尝试将诸如类的名称写入持久性XML文件

 <persistence-unit name="persistenceFirst" transaction-type="RESOURCE_LOCAL">
 <class>it.two.app.domain.first.OneTable</class>
 <exclude-unlisted-classes/>
</persistence-unit>

和it.two.app.domain.second.OtherTable

但是,当我运行Log时,说表’firstDB.other-table’不存在,我将其用于服务文件

@PersistenceContext(unitName ="persistenceFirst")
private EntityManager em;

@PersistenceContext(unitName = "persistenceSecond")
EntityManager em;

你有一些想法吗?Thi是数据源XML文件

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:jpa="http://www.springframework.org/schema/data/jpa"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.1.xsd
    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
    http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.0.xsd
    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
    http://www.springframework.org/schema/context     http://www.springframework.org/schema/context/spring-context-3.1.xsd">

<!-- first datasource -->
<context:property-placeholder location="classpath:jdbc-first.properties"/>
<bean id="dataSourceFirst"  class="org.apache.commons.dbcp.BasicDataSource"
    p:driverClassName="${jdbc.driverClassName}" p:url="${jdbc.databaseurl}"
    p:username="${jdbc.username}" p:password="${jdbc.password}"  />
<!-- second datasource -->      
<context:property-placeholder location="classpath:jdbc-second.properties"/>
<bean id="dataSourceSecond"     class="org.apache.commons.dbcp.BasicDataSource"
    p:driverClassName="${jdbc.driverClassName}" p:url="${jdbc.databaseurl}"
    p:username="${jdbc.username}" p:password="${jdbc.password}"  />


<bean id="transactionManagerFirst"  class="org.springframework.orm.jpa.JpaTransactionManager"
p:entityManagerFactory-ref="emfFirst"/>

<bean id="transactionManagerSecond"     class="org.springframework.orm.jpa.JpaTransactionManager"
p:entityManagerFactory-ref="emfSecond"/>

<tx:annotation-driven transaction-manager="transactionManagerFirst"/>
<tx:annotation-driven transaction-manager="transactionManagerSecond"/>


<jpa:repositories base-package="it.two.app.repository.first"
entity-manager-factory-ref="emfFirst" transaction-manager-ref="transactionManagerFirst" />

<jpa:repositories base-package="it.two.app.repository.second"
entity-manager-factory-ref="emfSecond" transaction-manager-ref="transactionManagerSecond" />

<bean id="emfFirst"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="persistenceUnitName" value="persistenceFirst"/>
<property name="persistenceXmlLocation" value="classpath:/META-INF/persistence-first.xml"/>
<property name="jpaVendorAdapter">
    <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" />
</property>

<property name="dataSource" ref="dataSourceFirst" />
<property name="packagesToScan" value="it.two.app.domain.first" />
<property name="jpaProperties">
    <props>
        <prop     key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
        <prop key="hibernate.max_fetch_depth">3</prop>
        <prop key="hibernate.jdbc.fetch_size">50</prop>
        <prop key="hibernate.jdbc.batch_size">10</prop>
    </props>
</property>

</bean>

<bean id="emfSecond"     class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="persistenceUnitName" value="persistenceSecond"/>
<property name="persistenceXmlLocation" value="classpath:/META-INF/persistence-        second.xml"/>
<property name="jpaVendorAdapter" >
 <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"/>
</property>
<property name="dataSource" ref="dataSourceSecond"/>
<property name="packagesToScan" value="it.two.app.domain.second"/>
<property name="jpaProperties">
 <props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop> 
<prop key="hibernate.max_fetch_depth">3</prop>
<prop key="hibernate.jdbc.fetch_size">50</prop>
<prop key="hibernate.jdbc.batch_size">10</prop>
 </props>
</property>

</bean>

</beans>

解!!!!!!我不明白这个问题。只是

<!-- first datasource -->
 <bean id="dataSourceFirst"  class="org.apache.commons.dbcp.BasicDataSource"
    p:driverClassName="com.mysql.jdbc.Driver" p:url="url...."
    p:username="username" p:password="password"  />
<!-- second datasource -->      
<bean id="dataSourceSecond"     class="org.apache.commons.dbcp.BasicDataSource"
     p:driverClassName="com.mysql.jdbc.Driver" p:url="url2...."
    p:username="username2" p:password="password2"  />

阅读 263

收藏
2020-06-20

共1个答案

小编典典

如果您想使用多个DataSourceSpring + JPA

  1. 创建两个或更多的PersistenceUnitpersistence.xml
  2. 创建EntityManagerFactory每个PersistenceUnitspring-beans.xml

在您的DAO类中。

@PersistenceContext(unitName ="JPA_1")
private EntityManager em_1;

@PersistenceContext(unitName ="JPA_2")
private EntityManager em_2;

Conig persistence.xml

<persistence-unit name="JPA_1" type="RESOURCE_LOCAL">
....
</persistence-unit>


<persistence-unit name="JPA_2" type="RESOURCE_LOCAL">
....
</persistence-unit>

配置:spring-beans.xml

<bean id="entityManagerFactory1" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    <property name="persistenceUnitName" value="JPA_1"/>
    <property name="jpaVendorAdapter" ref="jpaVendorAdapter"/>
    <property name="jpaDialect">
        <bean class="org.springframework.orm.jpa.vendor.EclipseLinkJpaDialect"/> <--if it is necessary, replace with hibernate.
    </property>
    <property name="jpaPropertyMap">
        <props>
            <prop key="eclipselink.weaving">false</prop> <--if it is necessary, replace with hibernate.
        </props>
    </property>
    <property name="loadTimeWeaver">
        <bean class="org.springframework.instrument.classloading.InstrumentationLoadTimeWeaver">
        </bean>
    </property>
</bean>

<bean id="entityManagerFactory2" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    <property name="persistenceUnitName" value="JPA_2"/>
    <property name="jpaVendorAdapter" ref="jpaVendorAdapter"/>
    <property name="jpaDialect">
        <bean class="org.springframework.orm.jpa.vendor.EclipseLinkJpaDialect"/> <--if it is necessary, replace with hibernate.
    </property>
    <property name="jpaPropertyMap">
        <props>
            <prop key="eclipselink.weaving">false</prop> <--if it is necessary, replace with hibernate.
        </props>
    </property>
    <property name="loadTimeWeaver">
        <bean class="org.springframework.instrument.classloading.InstrumentationLoadTimeWeaver">
        </bean>
    </property>
</bean>

<bean id="jpaVendorAdapter" class="org.springframework.orm.jpa.vendor.EclipseLinkJpaVendorAdapter"> <--if it is necessary, replace with hibernate.
    <property name="databasePlatform" value="org.eclipse.persistence.platform.database.MySQLPlatform"/>
    <!--<property name="databasePlatform" value="org.eclipse.persistence.platform.database.OraclePlatform" />-->
    <property name="generateDdl" value="false"/>
    <property name="showSql" value="true"/>
</bean>
2020-06-20