我需要用 springdata 查询两个数据库。这是我的配置:
Cosmos-dao.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop"
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-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/data/jpa
http://www.springframework.org/schema/data/jpa/spring-jpa.xsd">
<context:annotation-config />
<!--
////////////// DATA SOURCES ///////////
-->
<bean id="orbeDataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="org.hibernate.ejb.HibernatePersistence" />
<property name="url" value="jdbc:oracle:thin:@bddesarrollo.ccd.junta-andalucia.es:1526:DESA10G" />
<property name="username" value="orbe" />
<property name="password" value="etorbi" />
</bean>
<bean id="cosmosDataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="org.hibernate.ejb.HibernatePersistence" />
<property name="url" value="jdbc:oracle:thin:@bddesarrollo.ccd.junta-andalucia.es:1526:DESA10G" />
<property name="username" value="cosmos" />
<property name="password" value="cosmos" />
</bean>
<!--
//////////// ENTITY MANAGER FACTORY ///////////
-->
<bean id="orbeEntityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="orbeDataSource" />
<property name="loadTimeWeaver">
<bean class="org.springframework.instrument.classloading.InstrumentationLoadTimeWeaver" />
</property>
<property name="packagesToScan" value="es.juntadeandalucia.ccul.cosmos.modelodatos.repositorios.orbe" />
<property name="jpaVendorAdapter" ref="jpaVendorAdapter" />
</bean>
<bean id="cosmosEntityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="cosmosDataSource" />
<property name="loadTimeWeaver">
<bean class="org.springframework.instrument.classloading.InstrumentationLoadTimeWeaver" />
</property>
<property name="packagesToScan" value="es.juntadeandalucia.ccul.cosmos.modelodatos.repositorios.comos" />
<property name="jpaVendorAdapter" ref="jpaVendorAdapter" />
</bean>
<!--
/////////// TRANSACTION MANAGER PER DATASOURCE ////////////
-->
<bean id="orbeTransactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="orbeEntityManagerFactory" />
</bean>
<bean id="cosmosTransactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="orbeEntityManagerFactory" />
</bean>
<tx:annotation-driven transaction-manager="orbeTransactionManager" />
<tx:annotation-driven transaction-manager="cosmosTransactionManager" />
<jpa:repositories base-package="es.juntadeandalucia.ccul.cosmos.modelodatos.repositorios.orbe"
transaction-manager-ref="orbeTransactionManager"
entity-manager-factory-ref="orbeEntityManagerFactory" >
</jpa:repositories>
<jpa:repositories base-package="es.juntadeandalucia.ccul.cosmos.modelodatos.repositorios.cosmos"
transaction-manager-ref="cosmosTransactionManager"
entity-manager-factory-ref="cosmosEntityManagerFactory" >
</jpa:repositories>
<bean id="jpaVendorAdapter" class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<property name="databasePlatform" value="org.hibernate.dialect.Oracle10gDialect" />
</bean>
</beans>
我使用了两个数据源,entitimanager 和 transactionManager。我关注帖子:
http://blog.springsource.org/2011/04/26/advanced-spring-data-jpa-specifications-and-querydsl/(Oliver Gierke 回复)和
具有多个数据源但只有一组存储库的 Spring Data + JPA
持久性.xml
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="1.0"
xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence
http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd">
<persistence-unit name="cosmosPU" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<properties>
<property name="hibernate.ejb.naming_strategy" value="org.hibernate.cfg.DefaultNamingStrategy" />
<property name="hibernate.dialect" value="org.hibernate.dialect.Oracle10gDialect"/>
<property name="hibernate.show_sql" value="false"/>
<property name="hibernate.format_sql" value="true"/>
<!-- <property name="hibernate.hbm2ddl.auto" value="update" /> -->
</properties>
</persistence-unit>
<persistence-unit name="orbePU" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<properties>
<property name="hibernate.ejb.naming_strategy" value="org.hibernate.cfg.DefaultNamingStrategy" />
<property name="hibernate.dialect" value="org.hibernate.dialect.Oracle10gDialect"/>
<property name="hibernate.show_sql" value="false"/>
<property name="hibernate.format_sql" value="true"/>
<!-- <property name="hibernate.hbm2ddl.auto" value="update" /> -->
</properties>
</persistence-unit>
<!--
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<jta-data-source>jdbc/cosmosDS</jta-data-source>
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.Oracle10gDialect"/>
<property name="hibernate.show_sql" value="true"/>
<property name="hibernate.format_sql" value="true"/>
</properties>
-->
</persistence>
我的存储库:
package es.juntadeandalucia.ccul.cosmos.modelodatos.repositorios.orbe;
import java.util.List;
import org.springframework.data.jpa.repository.JpaRepository;
import es.juntadeandalucia.ccul.cosmos.modelodatos.entidades.orbe.Categoria;
import es.juntadeandalucia.ccul.cosmos.modelodatos.entidades.orbe.Categorizacion;
/**
* <b>Proyecto:</b> COSMOS<br>
* <b>Package:</b> es.juntadeandalucia.ccul.cosmos.modelodatos.repositorios<br>
* <b>Fecha</b>: 28-05-2012<br>
* Interfaz del repositorio de la entidad Categoria
*/
public interface RepositorioCategoria extends JpaRepository<Categoria, Long>, RepositorioCategoriaCustom{
public List<Categoria> findByCtzXCategorizacion(Categorizacion categorizacion);
}
package es.juntadeandalucia.ccul.cosmos.modelodatos.repositorios.orbe;
/**
* <b>Proyecto:</b> COSMOS<br>
* <b>Package:</b> es.juntadeandalucia.ccul.cosmos.modelodatos.repositorios<br>
* <b>Fecha</b>: 28-05-2012<br>
* Interfaz del repositorio de la entidad Categoria
*/
public interface RepositorioCategoriaCustom {
}
执行:
package es.juntadeandalucia.ccul.cosmos.modelodatos.repositorios.orbe.impl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.jpa.repository.support.QueryDslRepositorySupport;
import es.juntadeandalucia.ccul.cosmos.modelodatos.repositorios.orbe.RepositorioCategoriaCustom;
/**
* <b>Proyecto:</b> COSMOS<br>
* <b>Package:</b> es.juntadeandalucia.ccul.cosmos.modelodatos.repositorios.impl<br>
* <b>Fecha</b>: 28-05-2012<br>
* Clase que implementa la interfaz del repositorio de la entidad Categoria
*/
public class RepositorioCategoriaImpl extends QueryDslRepositorySupport implements RepositorioCategoriaCustom {
protected Logger log = LoggerFactory.getLogger(getClass());
}
为什么我会收到此错误?
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'categoriasBOImpl': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private es.juntadeandalucia.ccul.cosmos.modelodatos.repositorios.orbe.RepositorioCategoria es.juntadeandalucia.ccul.cosmos.negocio.bo.impl.CategoriasBOImpl.repositorioCategoria; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'repositorioCategoria': Cannot resolve reference to bean 'repositorioCategoriaImpl' while setting bean property 'customImplementation'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'repositorioCategoriaImpl': 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.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:287)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1106)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:517)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:294)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:225)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:291)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:585)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:913)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:464)
at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:384)
at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:283)
at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:111)
at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:3795)
at org.apache.catalina.core.StandardContext.start(StandardContext.java:4252)
at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1014)
at org.apache.catalina.core.StandardHost.start(StandardHost.java:736)
at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1014)
at org.apache.catalina.core.StandardEngine.start(StandardEngine.java:443)
at org.apache.catalina.core.StandardService.start(StandardService.java:448)
at org.apache.catalina.core.StandardServer.start(StandardServer.java:700)
at org.apache.catalina.startup.Catalina.start(Catalina.java:552)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:295)
at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:433)
Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: private es.juntadeandalucia.ccul.cosmos.modelodatos.repositorios.orbe.RepositorioCategoria es.juntadeandalucia.ccul.cosmos.negocio.bo.impl.CategoriasBOImpl.repositorioCategoria; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'repositorioCategoria': Cannot resolve reference to bean 'repositorioCategoriaImpl' while setting bean property 'customImplementation'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'repositorioCategoriaImpl': 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.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:506)
at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:87)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:284)
... 28 more
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'repositorioCategoria': Cannot resolve reference to bean 'repositorioCategoriaImpl' while setting bean property 'customImplementation'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'repositorioCategoriaImpl': 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.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:328)
at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveValueIfNecessary(BeanDefinitionValueResolver.java:106)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1360)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1118)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:517)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:294)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:225)
at org.springframework.beans.factory.support.Abs26-nov-2012 12:29:22 org.apache.catalina.core.StandardContext start
GRAVE: Error listenerStart
26-nov-2012 12:29:22 org.apache.catalina.core.StandardContext start
GRAVE: Falló en arranque del Contexto [/cosmos] debido a errores previos
tractBeanFactory.doGetBean(AbstractBeanFactory.java:291)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.findAutowireCandidates(DefaultListableBeanFactory.java:848)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:790)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:707)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:478)
... 30 more
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'repositorioCategoriaImpl': 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)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1106)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:517)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:294)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:225)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:291)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193)
at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:322)
... 43 more
Caused by: 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.findDefaultEntityManagerFactory(PersistenceAnnotationBeanPostProcessor.java:537)
at org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor.findEntityManagerFactory(PersistenceAnnotationBeanPostProcessor.java:496)
at org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor$PersistenceElement.resolveEntityManager(PersistenceAnnotationBeanPostProcessor.java:657)
at org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor$PersistenceElement.getResourceToInject(PersistenceAnnotationBeanPostProcessor.java:630)
at org.springframework.beans.factory.annotation.InjectionMetadata$InjectedElement.inject(InjectionMetadata.java:159)
at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:87)
at org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor.postProcessPropertyValues(PersistenceAnnotationBeanPostProcessor.java:339)
... 51 more
任何帮助是极大的赞赏。