Spring + JPA + Hibernate多个数据库

时间:2016-11-24 11:20:33

标签: spring hibernate jpa

我正在尝试使用Spring和JPA连接到两个数据库。我已经连接到一个数据库(sql server 2012),我需要连接到另一个数据库而不需要更改太多东西。我有app-context-dao.xml文件,它保存了我的hibernate和JPA配置:

<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

我的工厂bean看起来像这样:

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;
    }
}

1 个答案:

答案 0 :(得分:0)

您必须制作dataSourceentityManagerFactory主要版本之一。 primary = "true"

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