0

I have the following spring xml configuration:

  <bean id="entityManagerFactory1" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
            <property name="persistenceXmlLocation" value="classpath:/astra_persistence.xml" />
            <property name="persistenceUnitName" value="myPersistenceUnit1" />
            <property name="dataSource" ref="astraDataSource" />
            <property name="jpaVendorAdapter" ref="astraJpaVendorAdapter" />
            <property name="jpaDialect" ref="jpaDialect" />
  </bean>

<bean id="entityManagerFactory2" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean" primary="true">
            <property name="persistenceXmlLocation" value="classpath:/META-INF/persistence.xml" />
            <property name="persistenceUnitName" value="myPersistenceUnit2" />
            <property name="dataSource" ref="myDataSource" />
            <property name="jpaVendorAdapter" ref="jpaVendorAdapter" />
            <property name="jpaDialect" ref="jpaDialect" />

            <property name="jpaProperties">
                <props>
                    <prop key="hibernate.physical_naming_strategy">com.mypackage.domain.jpa.model.naming.ImprovedPhysicalNamingStrategy</prop>
                    <prop key="hibernate.implicit_naming_strategy">org.hibernate.boot.model.naming.ImplicitNamingStrategyJpaCompliantImpl</prop>
                </props>
            </property>

            <property name="packagesToScan"
                      value="com.mypackage" />
</bean>

In my DAO:

@PersistenceContext(unitName = "myPersistenceUnit2")
protected EntityManager myEntityManager;

And I can't set it up because of the following error:

NoUniqueBeanDefinitionException: No qualifying bean of type [javax.persistence.EntityManagerFactory] is defined

I tried:

  1. primary="true" in xml config - no luck
  2. unitName instead of name in @PersistenceContext - no luck

How to fix it?

1 Answer 1

1

Try to qialify your Em with appropriate name

import org.springframework.beans.factory.annotation.Qualifier;

@Qualifier("entityManagerFactory2")
@PersistenceContext(unitName = "myPersistenceUnit2")
protected EntityManager myEntityManager;
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.