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?