使用 Spring Data JPA 的多个数据库 [英] multiple databases with Spring Data JPA

查看:32
本文介绍了使用 Spring Data JPA 的多个数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将 Spring Data JPA 与项目中的 2 个数据库一起使用.但是当我尝试运行应用程序时会触发异常:

I'm trying to use Spring Data JPA with 2 databases in project. But an exception is triggered when I'm trying to run the application:

07:21:47.734 [main] ERROR o.s.web.context.ContextLoader - Context initialization failed
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'deviceRepository': Injection of persistence dependencies failed; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No unique bean of type [javax.persistence.EntityManagerFactory] is defined: expected single bean but found 2
    at org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor.postProcessPropertyValues(PersistenceAnnotationBeanPostProcessor.java:342) ~[spring-orm-3.1.0.RELEASE.jar:3.1.0.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1106) ~[spring-beans-3.1.0.RELEASE.jar:3.1.0.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:517) ~[spring-beans-3.1.0.RELEASE.jar:3.1.0.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456) ~[spring-beans-3.1.0.RELEASE.jar:3.1.0.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:294) ~[spring-beans-3.1.0.RELEASE.jar:3.1.0.RELEASE]
...

这是我的 applicationContext.xml

Here is my applicationContext.xml

<bean class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close" id="dataSource1">
    <property name="driverClassName" value="${database.driverClassName}"/>
    <property name="url" value="${database.url1}"/>
    <property name="username" value="${database.username1}"/>
    <property name="password" value="${database.password1}"/>
    <property name="testOnBorrow" value="true"/>
    <property name="testOnReturn" value="true"/>
    <property name="testWhileIdle" value="true"/>
    <property name="timeBetweenEvictionRunsMillis" value="1800000"/>
    <property name="numTestsPerEvictionRun" value="3"/>
    <property name="minEvictableIdleTimeMillis" value="1800000"/>
    <property name="validationQuery" value="SELECT 1"/>
</bean>
<bean class="org.springframework.orm.jpa.JpaTransactionManager" id="transactionManager1">
    <property name="entityManagerFactory" ref="entityManagerFactory1"/>
</bean>
<tx:annotation-driven mode="aspectj" transaction-manager="transactionManager1"/>
<bean class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean" id="entityManagerFactory1">
    <property name="persistenceUnitName" value="persistenceUnit1"/>
    <property name="dataSource" ref="dataSource1"/>
</bean>

<bean class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close" id="dataSource2">
    <property name="driverClassName" value="${database.driverClassName}"/>
    <property name="url" value="${database.url2}"/>
    <property name="username" value="${database.username2}"/>
    <property name="password" value="${database.password2}"/>
    <property name="testOnBorrow" value="true"/>
    <property name="testOnReturn" value="true"/>
    <property name="testWhileIdle" value="true"/>
    <property name="timeBetweenEvictionRunsMillis" value="1800000"/>
    <property name="numTestsPerEvictionRun" value="3"/>
    <property name="minEvictableIdleTimeMillis" value="1800000"/>
    <property name="validationQuery" value="SELECT 1"/>
</bean>
<bean class="org.springframework.orm.jpa.JpaTransactionManager" id="transactionManager2">
    <property name="entityManagerFactory" ref="entityManagerFactory2"/>
</bean>
<tx:annotation-driven mode="aspectj" transaction-manager="transactionManager2"/>
<bean class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean" id="entityManagerFactory2">
    <property name="persistenceUnitName" value="persistenceUnit2"/>
    <property name="dataSource" ref="dataSource2"/>
</bean>

这是我的 DAO 界面:

Here is my DAO interface:

@Repository
public interface DeviceRepository extends JpaRepository<Device, DevicePK>,
    JpaSpecificationExecutor<Device> {
}

我已经阅读了很多关于@PersistenceContext 的文章,但我从未见过 JpaRepository 的用法.

I've read a lot about @PersistenceContext but I never saw usages with JpaRepository.

推荐答案

必须将两个数据源之一定义为主.

One of the two datasource must be defined as primary.

有一个可以设置为 true 或 false 的主要属性:

<bean> has a primary attribute that can be set to true or false:

通常在 @Configuration 中,@Primary 注释被放置到:

Usually in @Configuration the @Primary annotation is placed to:

EntityManager数据源事务管理器

所以你可以尝试添加 primary="true"

到以下bean:

dataSource1transactionManager1entityManagerFactory1

这篇关于使用 Spring Data JPA 的多个数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆