Skip to content

findDefaultEntityManagerFactory should consider EMF bean's primary flag [SPR-7549] #12206

Closed

Description

@spring-projects-issues

Thomas Strecker opened SPR-7549 and commented

I am generally very happay with all the magic Spring provides me. This week, however, I ran into a problem I can't seem to solve, namely having two persistence units and therefore entity manager factories in my application's context.

While everything works perfect when I declare and annotate the EntityManager in all relevant classes with the @PersistenceContext(unitName="XYZ"), I am having a slightly different problem. While one of the persistence units' names is static (b/c I control it) the name of the other is dynamic. This may seem silly but is beyond my control.

So I tried to provide the one EMF with the "primary=true" and the other with the "primary=false" attributes, hoping that the context would inject the primary EMF into those classes where I didn't specify the name of the persistence unit, while the other classes would obtain the EMF specified through the unitName.

Unfortunately, PersistenceAnnotationBeanPostProcessor.findDefaultEntityManagerFactory() does not check for the "primary" attribute of the retrieved beans, therefore complaining that the default entity manager cannot be obtained.

I, therefore, suggest to implement this check in the post processor.


Affects: 3.0.4

Issue Links:

1 votes, 2 watchers

Activity

  1. spring-projects-issues commented on Sep 13, 2010

    @spring-projects-issues
    CollaboratorAuthor

    Thomas Strecker commented

    I just found the solution, and it already exists in the post processor. I was only ignorant because the name of the property is "defaultPersistenceUnitName". Therefore, I withdraw the request for improvement.

  2. spring-projects-issues commented on Sep 13, 2010

    @spring-projects-issues
    CollaboratorAuthor

    Juergen Hoeller commented

    Actually, supporting the primary flag for EntityManagerFactory lookup isn't a bad idea at all, in particular since "defaultPersistenceUnitName" requires a custom PersistenceAnnotationBeanPostProcessor definition. We'll consider this for Spring 3.1.

    Juergen

  3. spring-projects-issues commented on Sep 22, 2015

    @spring-projects-issues
    CollaboratorAuthor

    Juergen Hoeller commented

    Closing groups of outdated issues. Please reopen if still relevant.

  4. spring-projects-issues commented on Nov 12, 2015

    @spring-projects-issues
    CollaboratorAuthor

    Olivier Sambourg commented

    Hi,
    Still relevant for me (using Spring 4.0.x), using PersitenceAnnotationBeanPostProcessor requires turning off default annotation processing (which is a shame since we use java config) or naming our bean "org.springframework.context.annotation.internalPersistenceAnnotationProcessor" to override Spring's default one.

  5. spring-projects-issues commented on Aug 11, 2016

    @spring-projects-issues
    CollaboratorAuthor

    Juergen Hoeller commented

    This actually becomes more important now with Hibernate ORM 5.2 where a SessionFactory reference implements EntityManagerFactory directly, so Hibernate setups with direct SessionFactory exposure can now get mixed up with non-unique EntityManagerFactory matches. A primary flag is an easy way out, or alternatively autowire-candidate=false, both of which work now as of Spring Framework 4.3.3.

    Technically, a resolveNamedBean method on AutowireCapableBeanFactory does the general job now, including bean name exposure for dependency registration. PersistenceAnnotationBeanPostProcessor simply delegates to that method now.

  6. added
    in: data
    type: enhancement
    on Jan 11, 2019
  7. added this to the 4.3.3 milestone on Jan 11, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

in: datatype: enhancement

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions