EntityManagerFactoryUtils.findEntityManagerFactory() does not search parent contexts if persistence unit name is unspecified [SPR-10160] #14793
Labels
in: data
Issues in data modules (jdbc, orm, oxm, tx)
status: backported
An issue that has been backported to maintenance branches
type: enhancement
A general enhancement
Milestone
Tim Yates opened SPR-10160 and commented
Bottom line:
When EntityManagerFactoryUtils.findEntityManagerFactory() is called with a persistence unit name, it looks for an EntityManagerFactory bean through the whole context hierarchy. If the persistence unit name is omitted, it will only look at the current context and ignore beans in any parent context.
Why this matters:
I have a Spring MVC application that has a global context in /WEB-INF/applicationContext.xml and an MVC context in /WEB-INF/dispatcher-servlet.xml. The EntityManagerFactory is configured in the global context, and we have an OpenEntityManagerInViewInterceptor in the MVC context. If I leave the OEMIVInterceptor's persistenceUnitName property undefined, the application fails to start up with this error:
However, if I specify the persistenceUnitName, all is well. This also worked previously when all the beans were defined in one context (without the need to specify the persistence unit).
If this is not a bug, I fail to see the logic behind it. Here is the relevant snippet from EntityManagerFactoryUtils.findEntityManagerFactory():
As you can see, it calls the "...IncludingAncestors" method if the persistence unit name is defined, but uses the no-hierarchy version otherwise. This could easily be fixed by changing the second call to:
Affects: 3.1.3, 3.2 GA
Referenced from: commits 1a929f2, 50ed863
Backported to: 3.1.4
The text was updated successfully, but these errors were encountered: