|
#1
|
|||
|
|||
|
Can anyone tell me whether is it possible to use @PersistenceContext injection with multiple datasource? Spring has been giving me these errors:
Code:
Context initialization failed org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'adminService': Injection of persistence methods 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:324) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:996) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:470) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory$1.run(AbstractAutowireCapableBeanFactory.java:409) at java.security.AccessController.doPrivileged(Native Method) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:380) at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:264) at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:217) at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:261) at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:185) at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:164) at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:429) at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:729) at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:381) at org.springframework.web.context.ContextLoader.createWebApplicationContext(ContextLoader.java:255) at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:199) at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:45) at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:3764) at org.apache.catalina.core.StandardContext.start(StandardContext.java:4216) 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(Unknown Source) at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) at java.lang.reflect.Method.invoke(Unknown Source) 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.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:513) at org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor.findEntityManagerFactory(PersistenceAnnotationBeanPostProcessor.java:473) at org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor$PersistenceElement.resolveEntityManager(PersistenceAnnotationBeanPostProcessor.java:598) at org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor$PersistenceElement.getResourceToInject(PersistenceAnnotationBeanPostProcessor.java:569) at org.springframework.beans.factory.annotation.InjectionMetadata$InjectedElement.inject(InjectionMetadata.java:192) at org.springframework.beans.factory.annotation.InjectionMetadata.injectMethods(InjectionMetadata.java:117) at org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor.postProcessPropertyValues(PersistenceAnnotationBeanPostProcessor.java:321) ... 31 more |
|
#2
|
|||
|
|||
|
I suggest the javadocs... Specify the name of the context you want to inject.
__________________
Marten Deinum
Blog Use the [ code ] tags, young padawan |
|
#3
|
|||
|
|||
|
After checking the docs, I have changed the @PersistenceContext to @PersistenceUnit(name="emf") pointing to the specific EntityManagerFactory in applicationContext.xml but I'm still getting the same error as the first posting. Would appreciate anyone's help....
|
|
#4
|
|||
|
|||
|
You can specify your default persistence unit name in config file:
Code:
<!-- Read META-INF/persistence.xml in classpath -->
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalEntityManagerFactoryBean">
<property name="persistenceUnitName" value="my-unit-name" />
</bean>
__________________
::::::::::::::::::::::::::::::::::::::::::::::: Mickael Gervais Agaetis Site : www.agaetis.fr ::::::::::::::::::::::::::::::::::::::::::::::: |
|
#5
|
|||
|
|||
|
Thanks Mickael for your suggestion, however....I ran into another error. I manage to usage Mickael's suggestion and configure the entityManagerFactory to point to a persistenceUnitName (I have 2 entityManagerFactory and 2 persistence-unit). Tomcat 5.5 build without error but when I tried to test run, it output the following error
Code:
SEVERE: Servlet.service() for servlet jsp threw exception
org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'entityManagerFactory' is defined
at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeanDefinition(DefaultListableBeanFactory.java:387)
at org.springframework.beans.factory.support.AbstractBeanFactory.getMergedLocalBeanDefinition(AbstractBeanFactory.java:968)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:246)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:185)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:168)
at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:885)
at org.springframework.orm.jpa.support.OpenEntityManagerInViewFilter.lookupEntityManagerFactory(OpenEntityManagerInViewFilter.java:151)
at org.springframework.orm.jpa.support.OpenEntityManagerInViewFilter.lookupEntityManagerFactory(OpenEntityManagerInViewFilter.java:134)
at org.springframework.orm.jpa.support.OpenEntityManagerInViewFilter.doFilterInternal(OpenEntityManagerInViewFilter.java:93)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:215)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:188)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:213)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:174)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:117)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:108)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:174)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:874)
at org.apache.coyote.http11.Http11BaseProtocol$Http11ConnectionHandler.processConnection(Http11BaseProtocol.java:665)
at org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket(PoolTcpEndpoint.java:528)
at org.apache.tomcat.util.net.LeaderFollowerWorkerThread.runIt(LeaderFollowerWorkerThread.java:81)
at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:689)
at java.lang.Thread.run(Unknown Source)
the applicationContext looks like this: Code:
<bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" /> <bean id="emfOmstrade" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"> <property name="dataSource" ref="dsOmstrade" /> <property name="jpaVendorAdapter"> <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"> <property name="database" value="SQL_SERVER" /> <property name="showSql" value="true" /> </bean> </property> <property name="persistenceUnitName" value="omstradeManager" /> </bean> <bean id="emfOmsweb" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"> <property name="dataSource" ref="dsOmsweb" /> <property name="jpaVendorAdapter"> <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"> <property name="database" value="SQL_SERVER" /> <property name="showSql" value="true" /> </bean> </property> <property name="persistenceUnitName" value="omswebManager" /> </bean> <bean id="dsOmsweb" class="org.enhydra.jdbc.standard.StandardXADataSource"> <property name="transactionManager" ref="jotm" /> <property name="driverName" value="net.sourceforge.jtds.jdbc.Driver" /> <property name="url" value="jdbc:jtds:sqlserver://k" /> <property name="user" value="x" /> <property name="password" value="y" /> </bean> <bean id="dsOmstrade" class="org.enhydra.jdbc.standard.StandardXADataSource"> <property name="transactionManager" ref="jotm" /> <property name="driverName" value="net.sourceforge.jtds.jdbc.Driver" /> <property name="url" value="jdbc:jtds:sqlserver://o" /> <property name="user" value="x" /> <property name="password" value="y" /> </bean> <bean id="jotm" class="org.springframework.transaction.jta.JotmFactoryBean"/> <bean id="txManager" class="org.springframework.transaction.jta.JtaTransactionManager"> <property name="userTransaction" ref="jotm" /> </bean> <context:annotation-config /> <tx:annotation-driven transaction-manager="txManager" /> Code:
<persistence-unit name="omswebManager">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<properties>
<property name="hibernate.show_sql" value="true" />
<property name="hibernate.format_sql" value="true" />
<property name="hibernate.connection.driver_class"
value="net.sourceforge.jtds.jdbc.Driver" />
<property name="hibernate.connection.url"
value="jdbc:jtds:sqlserver://k" />
<property name="hibernate.connection.username" value="x" />
<property name="hibernate.connection.password" value="y" />
<property name="hibernate.dialect"
value="org.hibernate.dialect.SQLServerDialect" />
</properties>
</persistence-unit>
<persistence-unit name="omstradeManager">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<properties>
<property name="hibernate.show_sql" value="true" />
<property name="hibernate.format_sql" value="true" />
<property name="hibernate.connection.driver_class"
value="net.sourceforge.jtds.jdbc.Driver" />
<property name="hibernate.connection.url"
value="jdbc:jtds:sqlserver://o" />
<property name="hibernate.connection.username" value="x" />
<property name="hibernate.connection.password" value="y" />
<property name="hibernate.dialect"
value="org.hibernate.dialect.SQLServerDialect" />
</properties>
</persistence-unit>
Code:
import org.springframework.transaction.annotation.Transactional;
@Transactional
public class AdminService {
private EntityManager em;
@PersistenceContext(unitName="omswebManager")
public void setEm(EntityManager em) {
this.em = em;
}
|
|
#6
|
|||
|
|||
|
try to rename emfOmstrade to entityManagerFactory
Code:
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"> <property name="dataSource" ref="dsOmstrade" /> <property name="jpaVendorAdapter"> <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"> <property name="database" value="SQL_SERVER" /> <property name="showSql" value="true" /> </bean> </property> <property name="persistenceUnitName" value="omstradeManager" /> </bean>
__________________
::::::::::::::::::::::::::::::::::::::::::::::: Mickael Gervais Agaetis Site : www.agaetis.fr ::::::::::::::::::::::::::::::::::::::::::::::: |
|
#7
|
|||
|
|||
|
I recently battled Spring with this problem and found a way to make it work:
http://forum.springframework.org/showthread.php?t=60729 Note that I don't mention a datasource in applicationContext.xml because the JTA jndi datasource is defined in persistence.xml |
|
#8
|
|||
|
|||
|
Hi Ryan,
Thanks for the insights. Can you post your persistence.xml as well? |
![]() |
| Thread Tools | |
| Display Modes | |
|
|