Java – way to give persistenceUnitName for Spring’s LocalContainerEntityManagerFactoryBean without persistence.xml

hibernatejavajpa-2.0spring

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

Best Answer

Specifying property in LocalContainerEntityManagerFactoryBean works for me

    <property name="persistenceUnitName" value="MyPersistenceUnit"/>
Related Topic