2017-09-29 12 views
0

xml applicationContext.xmlに2つのdataSourceを設定しようとすると問題が発生します。 例では、Beanアノテーションを使用して設定を確認しています。 実際のアーキテクチャではxmlで設定が必要です。SpringDataを使用してBeanアノテーションなしで2つのデータベースを設定するとエラーが発生する

私はチュートリアルを見た:

http://www.baeldung.com/spring-data-jpa-multiple-databases

https://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#howto-two-datasources

をしかし、私は、この春のページの使用注釈で使用される方法を私の問題を解決しません。私は注釈を使用することはできません、私の構成はXMLにあります。 seconfデータソースを適用しようとするとエラーが発生します。

2番目のデータソースを追加する前に、問題はありません。 2番目のデータソースを追加すると機能しません。

My applicationContext.xml :

<?xml version="1.0" encoding="UTF-8"?> 
<beans 
    xmlns="http://www.springframework.org/schema/beans" 
    xmlns:aop="http://www.springframework.org/schema/aop" 
    xmlns:util="http://www.springframework.org/schema/util" 
    xmlns:task="http://www.springframework.org/schema/task" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:tx="http://www.springframework.org/schema/tx" 
    xmlns:jee="http://www.springframework.org/schema/jee" 
    xmlns:context="http://www.springframework.org/schema/context" 
    xmlns:jpa="http://www.springframework.org/schema/data/jpa" 
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
         http://www.springframework.org/schema/beans/spring-beans-4.2.xsd 
         http://www.springframework.org/schema/aop 
         http://www.springframework.org/schema/aop/spring-aop-4.2.xsd 
         http://www.springframework.org/schema/jee 
         http://www.springframework.org/schema/jee/spring-jee-4.2.xsd 
         http://www.springframework.org/schema/util 
         http://www.springframework.org/schema/util/spring-util-4.2.xsd 
         http://www.springframework.org/schema/tx 
         http://www.springframework.org/schema/tx/spring-tx-4.2.xsd 
         http://www.springframework.org/schema/task 
         http://www.springframework.org/schema/task/spring-task-4.2.xsd 
         http://www.springframework.org/schema/context 
         http://www.springframework.org/schema/context/spring-context-4.2.xsd 
         http://www.springframework.org/schema/data/jpa 
         http://www.springframework.org/schema/data/jpa/spring-jpa.xsd" 
    default-autowire="byName" default-lazy-init="true"> 

    <context:annotation-config /> 

    <context:component-scan base-package="br.com.myProject" /> 

    <jpa:repositories base-package="br.com.myProject.ged.repository"/> 

    <bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean" scope="singleton"> 
     <property name="jndiName" value="java:jboss/datasources/sgedDS" /> 
     <property name="resourceRef" value="true" />  
    </bean> 

    <bean id="dataSourceNurer" class="org.springframework.jndi.JndiObjectFactoryBean" scope="singleton"> 
     <property name="jndiName" value="java:jboss/datasources/nurerDS" /> 
     <property name="resourceRef" value="true" />  
    </bean> 


    <!-- ************** ENTITY MANAGER SGED ******************** --> 

    <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"> 
     <property name="packagesToScan" value="br.com.myProject.ged.entity" /> 
     <property name="dataSource" ref="dataSource" /> 
     <property name="jpaVendorAdapter"> 
      <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" /> 
     </property> 

     <property name="jpaProperties"> 
      <props> 
       <prop key="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</prop> 
       <prop key="hibernate.show_sql">true</prop> 
       <prop key="hibernate.format_sql">false</prop> 
      </props> 
     </property> 
    </bean> 

    <!-- ************** ENTITY NURER NURER ******************** -->  

    <bean id="entityManagerFactoryNurer" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"> 
     <property name="packagesToScan" value="br.com.myProject.ged.entity" /> 
     <property name="dataSource" ref="dataSourceNurer" /> 

     <property name="jpaVendorAdapter"> 
      <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" /> 
     </property> 

     <property name="jpaProperties"> 
      <props> 
       <prop key="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</prop> 
       <prop key="hibernate.show_sql">true</prop> 
       <prop key="hibernate.format_sql">false</prop> 
      </props> 
     </property> 
    </bean> 

    <!-- ******** SGED ******** --> 

    <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager"> 
     <property name="entityManagerFactory" ref="entityManagerFactory" /> 
    </bean> 

    <!-- ******** NURER ******** --> 

    <bean id="transactionManagerNurer" class="org.springframework.orm.jpa.JpaTransactionManager"> 
     <property name="entityManagerFactory" ref="entityManagerFactoryNurer" /> 
    </bean> 

    <tx:annotation-driven transaction-manager="transactionManager" /> <!-- SGED --> 
    <tx:annotation-driven transaction-manager="transactionManagerNurer" /> <!-- NURER --> 


    <bean id="persistenceExceptionTranslationPostProcessor" 
      class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor" /> 

    <bean class="org.springframework.beans.factory.config.CustomScopeConfigurer"> 
     <property name="scopes"> 
      <map> 
       <entry key="view"> 
        <bean class="br.com.myProject.ged.spring.SpringViewScope" /> 
       </entry> 
      </map> 
     </property> 
    </bean> 

</beans> 

My Bean Service layer:

@Transactional (transactionManager = "transactionManager2") 
    public List<DataBase2Entity> getAll(){ 
     return nurerSituacaoIdrRepository.findAll(); 
     // return new ArrayList<DataBase2Entity>(); 
    } 


@Transactional (transactionManager = "transactionManager") 
    public List<DataBaseEntity> getAll(){ 
     return nurerSituacaoIdrRepository.findAll(); 
     // return new ArrayList<DataBaseEntity>(); 
    } 

My BaseDao.java

public abstract class BaseDao<T> { 

    private Class<T> entityClass; 

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


    @PersistenceContext(unitName = "entityManagerFactoryNurer") 
    private EntityManager emNurer; 

    @SuppressWarnings("unchecked") 
    public BaseDao() { 
     this.entityClass = (Class<T>) ((ParameterizedType) getClass().getGenericSuperclass()).getActualTypeArguments()[0]; 
    } 

... 

UPDATE:2017年2月10日 ERROR実行時間:

Error creating bean with name 'entityManagerFactory' defined in ServletContext resource [/WEB-INF/applicationContext.xml]: Error setting property values 

同じentityManagerFactoryを使用するか、別のentityManagerFactoryを作成します(BaseDao.javaを参照)。

+0

エラーはどこにありますか? – Arun

+0

こんにちはアルン。私は更新しました。ありがとうございます。 –

答えて

0

これはXMLの問題であるようです。 私は.(ドット)は何の#105

<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager"> <property name="entityManagerFactory" ref="entityManagerFactory" /> </bean>.

ドットを削除して再試行してくださいは、ライン上で終わりではありません参照してください。

+0

ありがとうございます。エラーが発生した後エラー:ServletContextリソース[/WEB-INF/applicationContext.xml]で定義された 'entityManagerFactory'という名前のBeanを作成中にエラーが発生しました:プロパティ値の設定時にエラーが発生しました –

+0

同じエンティティマネージャを使用していますreturn error: SQL [n/a] –

+0

原因:org.hibernate.exception.SQLGrammarException:ResultSetを抽出できませんでした –

関連する問題