spring 获取错误未定义类型为 [javax.persistence.EntityManagerFactory] 的合格 bean:预期单个匹配 bean 但找到 2
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17833147/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
getting error No qualifying bean of type [javax.persistence.EntityManagerFactory] is defined: expected single matching bean but found 2
提问by Basit
I am spring spring 3.2. Here is my config file
我是春天春天3.2。这是我的配置文件
<bean id="legacyDataSource" name="legacydb" class="org.springframework.jdbc.datasource.DriverManagerDataSource" lazy-init="true">
<property name="driverClassName" value="${jdbc.legacy.driverClassName}" />
<property name="url" value="${jdbc.legacy.url}" />
<property name="username" value="${jdbc.legacy.username}" />
<property name="password" value="${jdbc.legacy.password}" />
</bean>
<bean id="ls360DataSource" name="Ls360db" class="org.springframework.jdbc.datasource.DriverManagerDataSource" lazy-init="true" >
<property name="driverClassName" value="${jdbc.ls360.driverClassName}" />
<property name="url" value="${jdbc.ls360.url}" />
<property name="username" value="${jdbc.ls360.username}" />
<property name="password" value="${jdbc.ls360.password}" />
</bean>
<bean id="legacyTransactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="legacyEmf"/>
</bean>
<bean id="ls360TransactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="ls360Emf"/>
</bean>
<tx:annotation-driven transaction-manager="transactionManager" />
<bean id="legacyEmf" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean" >
<property name="dataSource" ref="legacyDataSource" />
<property name="jpaVendorAdapter" ref="vendorAdaptor" />
<property name="packagesToScan" value="com.softech.ls360.integration.regulators.plcb.domain"/>
<property name="jpaProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.SQLServerDialect</prop>
<prop key="hibernate.max_fetch_depth">3</prop>
<prop key="hibernate.jdbc.fetch_size">50</prop>
<prop key="hibernate.jdbc.batch_size">10</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
</bean>
<bean id="ls360Emf" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean" >
<property name="dataSource" ref="ls360DataSource" />
<property name="jpaVendorAdapter" ref="vendorAdaptor" />
<property name="packagesToScan" value="com.softech.ls360.integration.regulators.plcb.domain"/>
<property name="jpaProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.SQLServerDialect</prop>
<prop key="hibernate.max_fetch_depth">3</prop>
<prop key="hibernate.jdbc.fetch_size">50</prop>
<prop key="hibernate.jdbc.batch_size">10</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
</bean>
<context:component-scan base-package="....db" />
Here is my class
这是我的课
@Service("dbManager")
@Repository
@Transactional
public class DatabaseManager {
@PersistenceContext
@Qualifier("legacyEmf")
private EntityManager legacyEm;
@PersistenceContext
@Qualifier("ls360Emf")
private EntityManager ls360Em;
@SuppressWarnings("unchecked")
@Transactional(readOnly=true)
public List<Object> getResultList(String query, Class mappingClass) throws Exception {
//Query emQuery = legacyEm.createNativeQuery(query, mappingClass);
//return emQuery.getResultList();
return null;
} //end of findTraineeFromLegacy()
}
Now when i rum the code i get the following error
现在,当我朗读代码时,出现以下错误
Error creating bean with name 'dbManager': Injection of persistence
dependencies failed; nested exception is
org.springframework.beans.factory.NoUniqueBeanDefinitionException:
No qualifying bean of type [javax.persistence.EntityManagerFactory] is defined:
expected single matching bean but found 2: legacyEmf,ls360Emf
Why i am getting this error. How can i solve it?
为什么我收到这个错误。我该如何解决?
Thanks
谢谢
回答by Volceri
I had the same issue today. Solved it doing the following:
我今天遇到了同样的问题。解决它执行以下操作:
First I've added the parameter unitName to @PersistenceContext to both entity manager properties:
首先,我将参数 unitName 添加到 @PersistenceContext 到两个实体管理器属性:
@PersistenceContext(unitName="appPU")
@Qualifier(value = "appEntityManagerFactory")
private EntityManager appEntityManager;
@PersistenceContext(unitName="managerPU")
@Qualifier(value = "managerEntityManagerFactory")
private EntityManager managerEntityManager;
And in my configuration file I've added a property persistenceUnitName to my bean definitions:
在我的配置文件中,我在 bean 定义中添加了一个属性 persistenceUnitName:
<bean id="appEntityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="dataSource1" />
<property name="persistenceUnitName" value="appPU" />
<property name="packagesToScan" value="br.com.app.domain" />
...
</bean>
<bean id="managerEntityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="dataSource2" />
<property name="persistenceUnitName" value="managerPU" />
<property name="packagesToScan" value="br.com.app.domain" />
...
</bean>
回答by Nickolay Davidenko
Also I'd like to add once more useful comment: you need to extend the section in the 'web.xml' file of your web-app. Since now you have 2 Entity Managers, you need 2 OpenEntityManagerInViewFilters. Look the example:
另外,我想再添加一条有用的评论:您需要扩展 web 应用程序的“web.xml”文件中的部分。由于现在您有 2 个实体管理器,因此您需要 2 个 OpenEntityManagerInViewFilters。看例子:
<filter>
<filter-name>OpenEntityManagerInViewFilter1</filter-name>
<filter-class>org.springframework.orm.jpa.support.OpenEntityManagerInViewFilter</filter-class>
<init-param>
<param-name>entityManagerFactoryBeanName</param-name>
<param-value>appEntityManagerFactory</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>OpenEntityManagerInViewFilter1</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<filter>
<filter-name>OpenEntityManagerInViewFilter2</filter-name>
<filter-class>org.springframework.orm.jpa.support.OpenEntityManagerInViewFilter</filter-class>
<init-param>
<param-name>entityManagerFactoryBeanName</param-name>
<param-value>managerEntityManagerFactory</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>OpenEntityManagerInViewFilter2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
Pay attention on fact the name 'appEntityManagerFactory' in < param-value >appEntityManagerFactory< / param-value > = 'appEntityManagerFactory' in < bean id="appEntityManagerFactory".
注意< param-value >appEntityManagerFactory</ param-value > = 'appEntityManagerFactory' in < bean id="appEntityManagerFactory"中的名称'appEntityManagerFactory'。
回答by Manish Prajapati
I also faced such problems and solved it. Please do the following to solve this error:
我也遇到过这样的问题并解决了。请执行以下操作来解决此错误:
Add the following line to all your entity classes of both schema.
将以下行添加到两个架构的所有实体类中。
@PersistenceContext(unitName="<persistenceUnit>")
transient EntityManager entityManager;
<persistenceUnit>is the name of the persistence unit you defined in the persistence.xmlfile.
<persistenceUnit>是您在persistence.xml文件中定义的持久性单元的名称。

