0

Scenario:

I have an application extending another application and so it has 2 DataSource and 2 EntityManagerFactory in it. The first EntityManagerFactory is created for the original application, and the second one is what is created and used in my extension. The application being extended uses many of its own jars and I am not looking at modifying any of the code in the original application's jars.

Bean definitions:


@Bean("em1")
@Primary
@PersistenceContext(unitName = "pc1")
public LocalContainerEntityManagerFactoryBean entityManagerFactory(
        @Qualifier("ds1") DataSource dataSource) {
    LocalContainerEntityManagerFactoryBean retVal = super.entityManagerFactory();
    retVal.setDataSource(myDataSource);
    retVal.setJpaProperties(Properties.getJpaProperties());
    return retVal;
}


@Bean(name = "em2")
@PersistenceContext(unitName = "pc2")
public LocalContainerEntityManagerFactoryBean entityManagerFactory(
        @Qualifier("ds2") DataSource dataSource) {
    LocalContainerEntityManagerFactoryBean emf = new LocalContainerEntityManagerFactoryBean();
    emf.setDataSource(dataSource);
    emf.setPersistenceXmlLocation("classpath:persistence/persistence.xml");
    return emf;
}

Issue:

I can successfully get an EntityManager from the second one in my classes by using

@PersistenceContext(unitName = "pc2")
protected EntityManager entityManager; 

However when the original application tries to get an EntityManager in one of its libraries by

@Autowired
private EntityManager myEntityManager;

it throws a NoUniqueBeanDefinitionException saying it found 2 beans of type EntityManager

Question:

How can I make my @Primary EntityManagerFactory create an EntityManager that is also used by default? Or what other solution can I do to fix these conflicting beans without modifying the original application?

Caleb
  • 67
  • 9
  • [@Order](https://stackoverflow.com/questions/30328897/what-is-the-use-of-order-annotation-in-spring) – bananas Nov 28 '19 at 04:29
  • @emotionlessbananas @ Order does not change the error – Caleb Nov 28 '19 at 04:46
  • Use @Qualifier to obtain respective Entity Manager – Swarit Agarwal Nov 28 '19 at 04:59
  • @SwaritAgarwal It's being autowired in the original application's jars. I don't want to have to manage my own version of those jars. – Caleb Nov 28 '19 at 05:28
  • use @Resource instead of Autowire and give the name of the bean as @Resource(name="em1") – ankidaemon Nov 28 '19 at 05:29
  • also I don't see where have you defined emf reference? – ankidaemon Nov 28 '19 at 05:31
  • @ankidaemon Good catch. The emf reference was at the class level and I forgot to copy it over. Fixed in code. I'm trying to avoid replacing the @ Autowired as it is in a jar in the original project. – Caleb Nov 28 '19 at 05:38
  • @primary should be working, if you've already taken care of all builds properly. Also check this out - https://github.com/spring-projects/spring-framework/pull/22711 – ankidaemon Nov 28 '19 at 05:55

0 Answers0