问题
I have multiple datasources and multiple EntityManagerFactories to be defined in my spring context.
If there is only on EntityManagerFactory we don't need to give persistenceUnitName. But to associate EntityManager to correct EntityManagerFactory with @PersistenceContext I should give unitName="somePU" attribute.
But if we give name for persistenceUnitName attribute then Spring is expecting these persistenceUnitName declarations in persistence.xml file.
Is there a way to give persistenceUnitName for Spring's LocalContainerEntityManagerFactoryBean without persistence.xml?
Also I found another issue when I used persistence.xml with Spring's LocalContainerEntityManagerFactoryBean. The packagesToScan property is not working. I need to list down all the Entity classes in persistence.xml.
Any idea why it is happening?
-siva
回答1:
Specifying property in LocalContainerEntityManagerFactoryBean
works for me
<property name="persistenceUnitName" value="MyPersistenceUnit"/>
回答2:
After creating the LocalEntityManagerFactoryBean, set the persistence unit name:
LocalContainerEntityManagerFactoryBean factory = new LocalContainerEntityManagerFactoryBean();
factory.setPersistenceUnitName("NAME_OF_PERSISTENCE_UNIT");
来源:https://stackoverflow.com/questions/12854913/is-there-a-way-to-give-persistenceunitname-for-springs-localcontainerentitymana