Is there a way to give persistenceUnitName for Spring's LocalContainerEntityManagerFactoryBean without persistence.xml?

倾然丶 夕夏残阳落幕 提交于 2020-01-24 03:06:45

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!