【问题标题】:spring jpa hibernate with more datasources具有更多数据源的spring jpa hibernate
【发布时间】:2012-10-18 07:14:37
【问题描述】:

我必须在我的应用程序(spring)中使用两个不同的数据库与 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

但是当我运行日志时说 表“firstDB.other-table”不存在 我使用到服务文件中

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

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

你有什么想法吗? 这是数据源 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"  />    

【问题讨论】:

    标签: spring hibernate jpa persistence-unit


    【解决方案1】:

    如果您想在Spring + JPA 中使用多个DataSource

    1. persistence.xml 中创建两个或多个PersistenceUnit
    2. spring-beans.xml 中的每个PersistenceUnit 创建EntityManagerFactory

    更多参考。

    1. Multiple database with Spring+Hibernate+JPA
    2. Access Multiple Database Using Spring 3, Hibernate 3
    3. Multiple Database using Spring 3.0 and Hibernate 3.0

    在你的 DAO 类中。

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

    配置持久化.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>
    

    【讨论】:

      猜你喜欢
      • 2014-12-06
      • 1970-01-01
      • 2014-08-21
      • 2010-12-26
      • 2016-05-26
      • 2021-09-09
      • 2016-09-03
      • 2012-01-31
      相关资源
      最近更新 更多