Dúvida Spring+Jpa com multiplos persistence units

2 respostas
R

Galera,estou tentando criar uma app Spring+JPA com 2 bancos de dados Postgresql,mas estou tendo esse erro.Alguem sabe onde estou errando?

Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No unique bean of type [javax.persistence.EntityManagerFactory] is defined: expected single bean but found 2

Esse é meu applicationContext.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"
	xsi:schemaLocation="http://www.springframework.org/schema/beans 
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd 
       http://www.springframework.org/schema/context 
http://www.springframework.org/schema/context/spring-context-2.5.xsd 
       http://www.springframework.org/schema/tx 
http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">

	<context:property-placeholder location="/WEB-INF/application.properties" />
	<bean id="entityManagerFactory1"
		class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
		<property name="persistenceUnitName" value="pu1" />
		<property name="persistenceXmlLocation" value="classpath:META-INF/persistence1.xml"/>
	</bean>
	
	<bean id="dataSource1" class="org.apache.commons.dbcp.BasicDataSource"
		destroy-method="close">
		<property name="driverClassName" value="${database.driver}" />
		<property name="url" value="${banco1.url}" />
		<property name="username" value="${database.username}" />
		<property name="password" value="${database.password}" />
	</bean>
	
	<bean id="entityManagerFactory2"
		class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
		<property name="persistenceUnitName" value="pu2" />
		<property name="persistenceXmlLocation" value="classpath:META-INF/persistence2.xml"/>
	</bean>
	
	<bean id="dataSource2" class="org.apache.commons.dbcp.BasicDataSource"
		destroy-method="close">
		<property name="driverClassName" value="${database.driver}" />
		<property name="url" value="${banco2.url}" />
		<property name="username" value="${database.username}" />
		<property name="password" value="${database.password}" />
	</bean>
	
	<bean id="txManager1" class="org.springframework.orm.jpa.JpaTransactionManager" >
		<property name="entityManagerFactory" ref="entityManagerFactory1" />
	</bean>
	
	<bean id="txManager2" class="org.springframework.orm.jpa.JpaTransactionManager">
		<property name="entityManagerFactory" ref="entityManagerFactory2" />
	</bean>
	
	<tx:annotation-driven transaction-manager="txManager1"/>
	<tx:annotation-driven transaction-manager="txManager2"/>
	
	<bean class="org.springframework.context.annotation.CommonAnnotationBeanPostProcessor" />
	<bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" />

	<bean id="contextApplicationContextProvider" class="context.ApplicationContextProvider" />
	<context:component-scan base-package="*" />
	</beans>

2 Respostas

tdisanti

Conseguiu resolver? Estou com o mesmo problema.

lindberg713

Aqui está a resposta para o problema de vocês. http://forum.springsource.org/showthread.php?63897-Multiple-EntityManagerFactory-beans-with-JTA-transaction-manager

Espero ter ajudado.

Criado 8 de abril de 2011
Ultima resposta 15 de mar. de 2012
Respostas 2
Participantes 3