Hello,
did something related to ApplicationContextAware
behaviour change between Spring Boot 3.0.6 and 3.1.0? I have custom Hibernate IdentifierGenerator
that also implements ApplicationContextAware
because it's POJO to access the spring context:
import org.hibernate.HibernateException;
import org.hibernate.engine.spi.SharedSessionContractImplementor;
import org.hibernate.id.IdentifierGenerator;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
@NonNullApi
public class SnowflakeIdentifierGenerator implements IdentifierGenerator, ApplicationContextAware {
private ApplicationContext applicationContext;
@Override
public Object generate(SharedSessionContractImplementor sharedSessionContractImplementor, Object o) throws HibernateException {
return applicationContext.getBean(SnowflakeIdGenerator.class).nextId(); // NPE happens here
}
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
this.applicationContext = applicationContext;
}
}
This works fine with Spring Boot 3.0.6, but after upgrading to 3.1.0, the ApplicationContext
is always null
.
Comment From: wilkinsona
Hibernate should call org.springframework.orm.hibernate5.SpringBeanContainer
to create the IdentifierGenerator
. This should result in setApplicationContext
being called on SnowflakeIdentifierGenerator
. If that's not happening, it may be that Hibernate isn't using the bean container for some reason. We upgraded to Hibernate 6.2 in Spring Boot 3.1 so there may be a change in Hibernate's behavior here. If you would like us to spend some more time investigating, please spend some time providing a complete yet minimal sample that reproduces the problem. You can share it with us by pushing it to a separate repository on GitHub or by zipping it up and attaching it to this issue.
Comment From: vaclavsvejcar
@wilkinsona Thanks for your feedback! I'll first then check Hibernate if there isn't any already reported issue with that and otherwise will prepare some minimum project demo to reproduce the issue.
Comment From: quaff
You have to set spring.jpa.properties.hibernate.cdi.extensions=true
, It's a breaking change at hibernate side.
https://github.com/hibernate/hibernate-orm/commit/a742f5e98882190959582d5a44d132c9973cd19b
Comment From: wilkinsona
Thanks, @quaff. HHH-16069 is the Hibernate issue that describes the change. It looks like it was made due to a problem in Wildfly. To my knowledge, we haven't seen that problem in a Spring Boot application so I think we should set hibernate.cdi.extensions=true
by default.
Comment From: quaff
Thanks, @quaff. HHH-16069 is the Hibernate issue that describes the change. It looks like it was made due to a problem in Wildfly. To my knowledge, we haven't seen that problem in a Spring Boot application so I think we should set
hibernate.cdi.extensions=true
by default.
I think It's better done in HibernateJpaVendorAdapter
at framework side.
Comment From: wilkinsona
That's certainly worth considering. Thanks for the suggestion. I'll ask the Framework team.
Comment From: wilkinsona
Just to hopefully reduce any potential confusion, as I understand it hibernate.cdi.extensions=true
is only needed for Hibernate to load extensions through SpringBeanContainer
. Without this setting, converters and listeners were still loaded from SpringBeanContainer
.
Comment From: 1dEraNCeSIv0
For anyone else stumbling upon this, wondering why their extensions are not loaded via the SpringBeanContainer
despite setting hibernate.cdi.extensions=true
:
The implementation of HHH-16069 has a bug, as reported in HHH-16935, affecting Hibernate since version 6.2 and hence Spring Boot since version 3.1. At the time of writing there's no fix on Hibernate's side yet, as far as I could find anyway.
The result of this bug is that when building an entity manager via the EntityManagerFactoryBuilder
the flag will actually be inverted before applying, Hibernate's MetadataBuilderImpl.disallowExtensionsInCdi
returns true
if hibernate.cdi.extensions=true
whereas it should return false
.
I cannot tell if hibernate.cdi.extensions=true
works correctly for use cases outside of this one (or if there are any other use cases).
Comment From: quaff
@1dEraNCeSIv0 I've created https://github.com/hibernate/hibernate-orm/pull/7049.
Comment From: quaff
@1dEraNCeSIv0 I've created hibernate/hibernate-orm#7049.
It's merged into main branch by commit https://github.com/hibernate/hibernate-orm/commit/f309a88552ef8ccd9f47fe4214641ece151e472c, and not backported to 6.2.x and 6.3.x yet.