spring jpa hibernate有更多的数据源

时间:2012-10-28 22:39:24

标签: spring hibernate jpa persistence-unit

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

但是当我运行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"  />    

1 个答案:

答案 0 :(得分:7)

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

  1. PersistenceUnit中创建两个或多个persistence.xml
  2. EntityManagerFactory中的每个PersistenceUnit创建spring-beans.xml
  3. 更多参考资料。

    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
    4. 在您的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>