17

我是春春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" />

这是我的课

@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()
}

现在,当我朗姆代码时,我收到以下错误

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

为什么我收到此错误。我该如何解决?

谢谢

4

3 回答 3

23

我今天有同样的问题。通过以下方式解决了它:

首先,我将参数 unitName 添加到 @PersistenceContext 到两个实体管理器属性:

@PersistenceContext(unitName="appPU")
@Qualifier(value = "appEntityManagerFactory")
private EntityManager appEntityManager;

@PersistenceContext(unitName="managerPU")
@Qualifier(value = "managerEntityManagerFactory")
private EntityManager managerEntityManager;

在我的配置文件中,我在我的 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>
于 2013-08-06T07:01:10.733 回答
4

此外,我想再添加一个有用的评论:您需要扩展您的网络应用程序的“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>

注意 <param-value >appEntityManagerFactory</ param-value > = 'appEntityManagerFactory' in < bean id="appEntityManagerFactory" 中的名称'appEntityManagerFactory'。

于 2013-09-12T10:39:14.437 回答
2

我也遇到过这样的问题并解决了。请执行以下操作来解决此错误:

将以下行添加到两个模式的所有实体类中。

@PersistenceContext(unitName="<persistenceUnit>")
transient EntityManager entityManager;

<persistenceUnit>persistence.xml是您在文件中定义的持久性单元的名称。

于 2014-02-25T06:17:51.357 回答