Skip to content
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

SpringBeanContainer not called with Hibernate ORM 6.2 #30545

Closed
vaclavsvejcar opened this issue May 24, 2023 · 10 comments
Closed

SpringBeanContainer not called with Hibernate ORM 6.2 #30545

vaclavsvejcar opened this issue May 24, 2023 · 10 comments
Assignees
Labels
in: data Issues in data modules (jdbc, orm, oxm, tx) type: bug A general bug
Milestone

Comments

@vaclavsvejcar
Copy link

vaclavsvejcar commented May 24, 2023

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.

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged or decided on label May 24, 2023
@wilkinsona
Copy link
Member

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.

@wilkinsona wilkinsona added the status: waiting-for-feedback We need additional information before we can continue label May 24, 2023
@vaclavsvejcar
Copy link
Author

@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.

@spring-projects-issues spring-projects-issues added status: feedback-provided Feedback has been provided and removed status: waiting-for-feedback We need additional information before we can continue labels May 24, 2023
@wilkinsona wilkinsona added status: waiting-for-feedback We need additional information before we can continue and removed status: feedback-provided Feedback has been provided labels May 24, 2023
@quaff
Copy link
Contributor

quaff commented May 25, 2023

You have to set spring.jpa.properties.hibernate.cdi.extensions=true, It's a breaking change at hibernate side.

hibernate/hibernate-orm@a742f5e

@spring-projects-issues spring-projects-issues added status: feedback-provided Feedback has been provided and removed status: waiting-for-feedback We need additional information before we can continue labels May 25, 2023
@wilkinsona
Copy link
Member

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.

@quaff
Copy link
Contributor

quaff commented May 25, 2023

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.

@wilkinsona
Copy link
Member

That's certainly worth considering. Thanks for the suggestion. I'll ask the Framework team.

@bclozel bclozel transferred this issue from spring-projects/spring-boot May 25, 2023
@bclozel bclozel added in: data Issues in data modules (jdbc, orm, oxm, tx) type: bug A general bug and removed status: waiting-for-triage An issue we've not yet triaged or decided on status: feedback-provided Feedback has been provided labels May 25, 2023
@bclozel bclozel added this to the 6.0.10 milestone May 25, 2023
@jhoeller jhoeller self-assigned this May 25, 2023
@jhoeller jhoeller changed the title ApplicationContextAware - context is null in Hibernate IdentifierGenerator after upgrading from 3.0.6 -> 3.1.0 SpringBeanContainer not called with Hibernate ORM 6.2 May 25, 2023
@wilkinsona
Copy link
Member

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.

mdeinum pushed a commit to mdeinum/spring-framework that referenced this issue Jun 29, 2023
@1dEraNCeSIv0
Copy link

1dEraNCeSIv0 commented Jul 22, 2023

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).

@quaff
Copy link
Contributor

quaff commented Jul 24, 2023

@1dEraNCeSIv0 I've created hibernate/hibernate-orm#7049.

@sbrannen sbrannen changed the title SpringBeanContainer not called with Hibernate ORM 6.2 SpringBeanContainer not called with Hibernate ORM 6.2 Aug 2, 2023
@quaff
Copy link
Contributor

quaff commented Oct 16, 2023

@1dEraNCeSIv0 I've created hibernate/hibernate-orm#7049.

It's merged into main branch by commit hibernate/hibernate-orm@f309a88, and not backported to 6.2.x and 6.3.x yet.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: data Issues in data modules (jdbc, orm, oxm, tx) type: bug A general bug
Projects
None yet
Development

No branches or pull requests

7 participants