Hi, I'm trying to setup two different persistence units with independent models. I've just get to work everything except Spring Data Rest. When tomcat starts it throws:
Doing some debugging I've found that in org.springframework.orm.jpa.support.PersistenceAnn otationBeanPostProcessor checks that only one EntityManagerFactory is present:Code: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) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1106) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:517) ...
Is there any way to tell Spring Data Rest which EntityManager should be used?Code:protected EntityManagerFactory findDefaultEntityManagerFactory(String requestingBeanName) throws NoSuchBeanDefinitionException{ String[] beanNames = BeanFactoryUtils.beanNamesForTypeIncludingAncestors(this.beanFactory, EntityManagerFactory.class); if (beanNames.length == 1) { String unitName = beanNames[0]; EntityManagerFactory emf = (EntityManagerFactory) this.beanFactory.getBean(unitName); if (this.beanFactory instanceof ConfigurableBeanFactory) { ((ConfigurableBeanFactory) this.beanFactory).registerDependentBean(unitName, requestingBeanName); } return emf; } else { throw new NoSuchBeanDefinitionException( EntityManagerFactory.class, "expected single bean but found " + beanNames.length); } }
My spring configuration is:
Thanks for your help.Code:<?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:jpa="http://www.springframework.org/schema/data/jpa" xmlns:repository="http://www.springframework.org/schema/data/repository" xsi:schemaLocation="http://www.springframework.org/schema/data/repository http://www.springframework.org/schema/data/repository/spring-repository-1.4.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd"> <context:annotation-config /> <bean id="dataSourceGeneral" class="org.springframework.jndi.JndiObjectFactoryBean"> <property name="jndiName" value="java:comp/env/jdbc/GeneralPOS" /> </bean> <bean id="entityManagerGeneral" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"> <property name="persistenceUnitName" value="generalPU"></property> <property name="dataSource" ref="dataSourceGeneral"></property> <property name="packagesToScan" value="es.infinitel.pos.domain.general"></property> <property name="jpaVendorAdapter"> <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"> <property name="showSql" value="true"></property> <property name="generateDdl" value="true"></property> <property name="databasePlatform" value="org.hibernate.dialect.MySQL5InnoDBDialect"></property> </bean> </property> <property name="jpaProperties"> <props> <prop key="hibernate.ejb.entitymanager_factory_name"> entityManagerGeneral </prop> </props> </property> </bean> <bean id="dataSourceRestUser" class="org.springframework.jndi.JndiObjectFactoryBean"> <property name="jndiName" value="java:comp/env/jdbc/UserPOS" /> </bean> <bean id="dataSourceClientes" class="es.infinitel.pos.backend.ClienteRoutingDataSource"> <property name="targetDataSources"> <map key-type="java.lang.String"> <entry key="restuser" value-ref="dataSourceRestUser"></entry> </map> </property> </bean> <bean id="entityManagerUser" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"> <property name="persistenceUnitName" value="userPU"></property> <property name="dataSource" ref="dataSourceClientes"></property> <property name="packagesToScan" value="es.infinitel.pos.domain.cliente"></property> <property name="jpaVendorAdapter"> <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"> <property name="showSql" value="true"></property> <property name="generateDdl" value="true"></property> <property name="databasePlatform" value="org.hibernate.dialect.MySQL5InnoDBDialect"></property> </bean> </property> <property name="jpaProperties"> <props> <prop key="hibernate.ejb.entitymanager_factory_name"> entityManagerUser </prop> </props> </property> </bean> <bean id="transactionManagerUser" class="org.springframework.orm.jpa.JpaTransactionManager"> <property name="entityManagerFactory" ref="entityManagerUser" /> </bean> <bean id="transactionManagerGeneral" class="org.springframework.orm.jpa.JpaTransactionManager"> <property name="entityManagerFactory" ref="entityManagerGeneral" /> </bean> <jpa:repositories base-package="es.infinitel.pos.repositories.cliente" entity-manager-factory-ref="entityManagerUser" > <repository:include-filter type="assignable" expression="es.infinitel.pos.repositories.cliente.IdentidadRepository"/> </jpa:repositories> <jpa:repositories base-package="es.infinitel.pos.repositories.general" entity-manager-factory-ref="entityManagerUser" > <repository:exclude-filter type="assignable" expression="es.infinitel.pos.repositories.cliente.IdentidadRepository"/> </jpa:repositories> <bean id="dummy" class="es.infinitel.pos.backend.Dummy"></bean> </beans>
Pepe Ribas


Reply With Quote
