복붙노트

[SPRING] 더 많은 데이터 소스를 가진 spring jpa hibernate

SPRING

더 많은 데이터 소스를 가진 spring jpa hibernate

내 응용 프로그램 (봄)에 Hibernate, Jpa와 함께 두 개의 다른 데이터베이스를 사용해야한다. 서로 다른 테이블을 다른 데이터 소스에 직접 정의하고 싶습니다. 그래서 두 개의 다른 지속성 단위를 사용하고

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

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

다른 테이블을 다른 패키지에 넣습니다. 그러나 그것은 작동하지 않습니다. 실제로 모든 테이블은 첫 번째 데이터 소스와 함께 있습니다. 그때 나는 perististence 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;

아이디어가 있습니까? 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. ==============================

    1.Spring + JPA에서 여러 개의 DataSource를 사용하고자한다면.

    Spring + JPA에서 여러 개의 DataSource를 사용하고자한다면.

    더 많은 참조.

    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>
    
  2. from https://stackoverflow.com/questions/13113739/spring-jpa-hibernate-with-more-datasources by cc-by-sa and MIT license