Skip to content

If an @EntityListener class is annotated with @Component then it is instantiated twice #27433

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
nightswimmings opened this issue Sep 17, 2021 · 3 comments
Labels
in: core Issues in core modules (aop, beans, core, context, expression) status: invalid An issue that we don't feel is valid

Comments

@nightswimmings
Copy link

I am trying to take benefit of this: #20852,

so I annotate my entity with @EntityListener(MyListener.class)

and then use the listener as:

public MyListener {

   @Autowired 
   AnotherDependency anotherDependency;

   @PostLoad
    public void onRead(Entity entity) {
            //
    }
}

with that, it seems we finally can get rid of the nasty static hack and get the bean spring-processed.

But as fine as this works for listener own dependencies, it did not work for me for autowiring the listener itself.
Looks like EntityManagerFactory/Hibernate manages it as if it were a Spring bean, but it does not exposes it in the context for another bean to inject it.

I checked documentation and I saw someone claimed the listener had to be annotated with @Component (although it worked in my case without it in regards to its internal autowires), and after I did this, the bean was managed by Spring and I could inject it, but then, I realized it was instantiated twice:

  • LocalEntityManagerFactory reads the @EntityListener annotation and instantiates it and sets up dependencies (and this instance is the only one that handles the events)
  • And then Spring seems to parse the @component and do the operation again (and this instance is the only one that gets injected when you @autowire the listener in some other bean)

Is this an expected behavior? Do I have a bug in my code? Is it an issue on Hibernate's side perhaps?

@quaff
Copy link
Contributor

quaff commented Sep 18, 2021

I think it's expected behavior. hibernate get bean from SpringBeanContainer with useJpaCompliantCreation=true, It will create new bean instead of get existed bean.
https://github.com/hibernate/hibernate-orm/blob/e76241a3091078713dd4b57de085f5fadce5e0db/hibernate-core/src/main/java/org/hibernate/resource/beans/internal/ManagedBeanRegistryImpl.java#L43-L46

@bclozel bclozel transferred this issue from spring-projects/spring-boot Sep 18, 2021
@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged or decided on label Sep 18, 2021
@nightswimmings
Copy link
Author

nightswimmings commented Sep 20, 2021

Hi @quaff ! Thanks for the point! Sadly, it seems it is a quite hardcoded setup, maybe prepared for future flexibility...
or is there any creative solution for it? I cannot post-process it because the value is hardcoded and not settable

@rstoyanchev rstoyanchev added the in: core Issues in core modules (aop, beans, core, context, expression) label Nov 10, 2021
@snicoll
Copy link
Member

snicoll commented Sep 26, 2023

You need to separate the two concepts as @quaff mentioned. Or you can disable the integration between Hibernate and Spring if you don't need it (but it looks like you are).

@snicoll snicoll closed this as not planned Won't fix, can't repro, duplicate, stale Sep 26, 2023
@snicoll snicoll added status: invalid An issue that we don't feel is valid and removed status: waiting-for-triage An issue we've not yet triaged or decided on labels Sep 26, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: core Issues in core modules (aop, beans, core, context, expression) status: invalid An issue that we don't feel is valid
Projects
None yet
Development

No branches or pull requests

5 participants