2012-03-27 82 views
1

我想在項目中使用Spring數據JPA與2數據庫。但異常被觸發時,我試圖運行應用程序:多個數據庫與彈簧數據JPA

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

<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接口:

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

我我已經閱讀了很多關於@PersistenceContext的內容,但是我從來沒有見過JpaRepository的用法。

回答

1

那麼你確實有2個entitymanagers。 我從來沒有使用過JPARepository,但是如果它在nosql的spring-data上的計數器部分接近工作,Spring會增強這個類,並且可能會注入EM。你的問題是你有2個EM申報。

看一看彈簧JPA文檔,他們會告訴你如何配置你怎麼可以添加特定EMF到您的回購

0

長話短說庫:

你必須創建該接口的自定義實現,並且實現了包含實體管理器聲明:

@PersistenceContext(unitName = "persistenceUnit1") 
    private EntityManager entityManager; 
如果你想使用持久性單元

1.

查看結果:http://docs.spring.io/spring-data/data-jpa/docs/current/reference/html/repositories.html#repositories.custom-implementations,它也有示例和示例1.17(稍微向下一頁)實現接口並在其中具有實體管理器,並將該實體管理器傳遞給超級構造器。

你也可以看看Spring Data - JPA, customizing Repository not working,它具有擴展JpaRepository的接口的實現,它也在實現中聲明瞭實體管理器。只需將unitName添加到@PersistenceContext註釋並嘗試這種方式。

你可能不需要任何自定義方法(這樣你的界面可以是空的),但你需要的是通過對實體管理器和所有與自己的界面延伸到旁路默認自動連線行爲修修補補的構造。