2016-11-24 71 views
0

我想用Spring和JPA連接兩個數據庫。我已經連接到一個數據庫(SQL Server 2012),並且我需要連接到另一個數據庫而不需要更改太多內容。我有對我的休眠的應用程序上下文,dao.xml文件和JPA配置:Spring + JPA + Hibernate多個數據庫

<beans xmlns="http://www.springframework.org/schema/beans" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" 
xmlns:context="http://www.springframework.org/schema/context" 
xmlns:util="http://www.springframework.org/schema/util" xmlns:tx="http://www.springframework.org/schema/tx" 
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:jdbc="http://www.springframework.org/schema/jdbc" 
xmlns:task="http://www.springframework.org/schema/task" 
xmlns:jee="http://www.springframework.org/schema/jee" 
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.xsd 
      http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd 
      http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd 
      http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd 
      http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd 
      http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security.xsd     
      http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee.xsd 
      http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task.xsd 
      http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd 
      http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"> 

<aop:aspectj-autoproxy proxy-target-class="false" /> 

<jee:jndi-lookup id="dataSource" jndi-name="jdbc/SQLCMS" expected-type="javax.sql.DataSource" /> 
<jee:jndi-lookup id="dataSourceOracle" jndi-name="jdbc/razmjenskaDBSQL" expected-type="javax.sql.DataSource" /> 

<bean id="entityManagerFactory" 
    class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">  
    <property name="dataSource" ref="dataSource" /> 
    <property name="packagesToScan" value="hr.akd.cms.*" /> 
    <property name="persistenceUnitName" value="caPersistenceUnit" /> 
    <property name="jpaVendorAdapter"> 
     <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">   
      <property name="generateDdl" value="false" />    
      <property name="databasePlatform" value="org.hibernate.dialect.SQLServer2012Dialect" /> 
      <property name="showSql" value="false" /> 
     </bean> 
    </property> 
    <!-- .................. 
     Initialize additional Hibernate JPA related properties 
     if required 
    .................. --> 
    <property name="jpaProperties"> 
     <props>     
      <prop key="hibernate.hbm2ddl.auto">update</prop>  
      <prop key="hibernate.show_sql">false</prop> 
      <prop key="hibernate.format_sql">true</prop>            
     </props> 
    </property> 
</bean> 

<bean id="entityManagerFactoryOracle" 
    class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">  
    <property name="dataSource" ref="dataSourceOracle" /> 
    <property name="persistenceUnitName" value="userPersistenceUnit" /> 
    <property name="packagesToScan" value="hr.akd.cms.*" /> 
    <property name="jpaVendorAdapter"> 
     <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">   
      <property name="generateDdl" value="false" />    
      <property name="databasePlatform" value="org.hibernate.dialect.SQLServer2012Dialect" /> 
      <property name="showSql" value="false" /> 
     </bean> 
    </property> 
    <!-- .................. 
     Initialize additional Hibernate JPA related properties 
     if required 
    .................. --> 
    <property name="jpaProperties"> 
     <props>      
      <prop key="hibernate.show_sql">false</prop> 
      <prop key="hibernate.format_sql">true</prop>            
     </props> 
    </property> 
</bean> 

<!-- .................. Transaction manager setup .................. --> 

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

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

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


<!-- .................. Spring data - declare base packages for scanning, 
    all classes extending from data repositories will be available for autowire 
    .................. -->  

<jpa:repositories base-package="hr.akd.cms.repository" 
    factory-class="hr.akd.cms.repository.impl.CMSCustomRepositoryFactoryBean" /> 

    <bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor"> 
    <property name="defaultPersistenceUnitName" value="caPersistenceUnit"/> 

</bean> 

我加entityManagerFactoryOracle,dataSourceOracle JNDI transactionManagerOracle我的第二個數據庫。當我開始我的應用程序,我得到這個

Injection of persistence dependencies failed; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [javax.persistence.EntityManagerFactory] is defined: expected single bean but found 2: entityManagerFactory,entityManagerFactoryOracle 

我廠豆看起來是這樣的:

public class CMSCustomRepositoryFactoryBean<R extends JpaRepository<T, I>, T, I extends Serializable> 
    extends JpaRepositoryFactoryBean<R, T, I> { 

@SuppressWarnings("rawtypes") 
protected RepositoryFactorySupport createRepositoryFactory(EntityManager entityManager) { 

    return new CMSSearchFactoryFactory(entityManager); 
} 

private static class CMSSearchFactoryFactory<T, I extends Serializable> extends JpaRepositoryFactory { 

    private EntityManager entityManager; 

    public CMSSearchFactoryFactory(EntityManager entityManager) { 
     super(entityManager); 

     this.entityManager = entityManager; 
    } 

    @SuppressWarnings("unchecked") 
    protected Object getTargetRepository(RepositoryMetadata metadata) { 

     return new CMSCustomRepositoryImpl<T, I>((Class<T>) metadata.getDomainType(), entityManager); 
    } 

    protected Class<?> getRepositoryBaseClass(RepositoryMetadata metadata) { 

     // The RepositoryMetadata can be safely ignored, it is used by the 
     // JpaRepositoryFactory 
     // to check for QueryDslJpaRepository's which is out of scope. 
     return CMSCustomRepository.class; 
    } 
} 

回答

0

你必須使dataSourceentityManagerFactory主之一。 primary = "true"

<bean id="entityManagerFactory" 
    class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean" 
    primary = "true"> 
+0

問題設置後仍然存在的主要=「真」 .. – damir9991

+0

你可以檢查它是否是相同的異常,或者說,對數據源..我不知道怎麼加屬性主要=「真」爲JNDI無論如何,豆聲明 – Sagar

+0

EntityManagerFactory發現同樣的例外2 – damir9991

相關問題