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

Support for Hibernate ORM 5.2 [SPR-14327] #18899

Closed
spring-projects-issues opened this issue Jun 2, 2016 · 18 comments
Closed

Support for Hibernate ORM 5.2 [SPR-14327] #18899

spring-projects-issues opened this issue Jun 2, 2016 · 18 comments
Assignees
Labels
in: data Issues in data modules (jdbc, orm, oxm, tx) type: enhancement A general enhancement
Milestone

Comments

@spring-projects-issues
Copy link
Collaborator

spring-projects-issues commented Jun 2, 2016

Juergen Hoeller opened SPR-14327 and commented

Hibernate ORM 5.2 got released just in time for Spring Framework 4.3. Let's do everything we can to support it right away, in particular upgrading our spring-orm-hibernate5 build to 5.2 while retaining compatbility with 5.0 and 5.1.


Issue Links:

0 votes, 11 watchers

@spring-projects-issues
Copy link
Collaborator Author

Juergen Hoeller commented

This turns out to be a significant effort, with plenty of deprecations to deal with - including a relocated Query type - and even a hard change in the Session.getFlushMode() signature. We're reflectively adapting to it for the time being, but as of Spring 5, we should really be requiring Hibernate 5.2+ for those reasons.

@spring-projects-issues
Copy link
Collaborator Author

Oliver Drotbohm commented

Any chance we can backport this to 4.2? With that in place and some tweaks that already got working in Spring Data, we could even get Spring Boot 1.3 projects to use Hibernate 5.2 successfully.

@spring-projects-issues
Copy link
Collaborator Author

Juergen Hoeller commented

I'd rather recommend a version override to Spring Framework 4.3 next to the Hibernate 5.2 override. The latter has way more implications than the former, and I'm not really keen on dragging the follow-up issues into our 4.2.x line.

@spring-projects-issues
Copy link
Collaborator Author

Hantsy Bai commented

In Hibernate 5.2 , It seemsSession(org.hibernate.Session) changed the getFlushMode() method return type to javax.persistence.FlushModeType.

I got failure when I used Spring boot 1.3 and Hibernate 5.2.0.Final.

Caused by: java.lang.NoSuchMethodError: org.hibernate.Session.getFlushMode()Lorg/hibernate/FlushMode;

@spring-projects-issues
Copy link
Collaborator Author

Oliver Drotbohm commented

The commits for that ticket fix exactly that. Note, that the tweaks only made it into Spring 4.3, not 4.2 (which is the version Boot 1.3 is currently based on).

Boot 1.3 with Hibernate 5 is going to be a tough call, also due to the many changes that were introduced in 5.0 and the necessary tweaks to the existing auto-configuration for JPA. So while there is a theoretical chance you get it to work by upgrading Spring Framework to 4.3 snapshots (as GA is not released yet) and disabling all JPA related auto configuration and configure it manually, we highly recommend to wait for the Spring Boot 1.4 RC coming next, which will be based on Spring Framework 4.3 GA and have all the necessary tweaks to the auto-configuration built in so that Hibernate 5 (in whatever minor version) should be a pleasant experience.

@spring-projects-issues
Copy link
Collaborator Author

Hantsy Bai commented

I am trying to upgrade to Spring Boot 1.4 M3, and got another issues the spring-boot-starter-data-jpa depends on hibernate-entitymanager, which is moved to Hibernate 5.2 core.

I have just noticed this issue. Most of the time, I used Hibernate as JPA provider by default.

Why spring-boot-starter-data-jpa depends on hibernate directly, not Java EE JPA API spec? which means we can not use OpenJPA, EclipseLink for JPA provider?

@spring-projects-issues
Copy link
Collaborator Author

Oliver Drotbohm commented

Hibernate 5.2 was released two days ago. The Boot milestone was released way before that. Plus, it's the Hibernate guys that decided to change the artifact structure in a minor release and thus render Hibernate 5.2 unusable with the currently released Spring Boot Data JPA starter.

The starter is just a convenience mechanism to get JPA up and running with the most popular JPA provider. Read up on the starters in Boot's reference documentation. That said, nothing forces you to use the starter in the first place. You could either stick to it, exclude Hibernate and pull in the persistence provider of your choice manually (like shown here or just don't use the starter at all and manually declare all dependencies you want.

Generally speaking, I think we're moving away from the purpose of the ticket, which was putting the necessary tweaks in Spring Framework in place to make sure it works with Hibernate 5.2. Everything that needs to be done in Spring Data JPA is tracked in DATAJPA-911, everything for Spring Boot is tracked in this ticket. For more general questions on how to use the starters, please use StackOverflow and the Spring Boot Gitter channel.

@spring-projects-issues
Copy link
Collaborator Author

Holger Stenzhorn commented

I have just upgraded our application to Spring 4.3 GA and Hibernate 5.2 and it seems that both are not yet working 100% together. The following bean declaration works fine with Spring 4.3 GA and Hibernate 5.1:

@Bean
@Primary
public EntityManagerFactory entityManagerFactory() throws IOException {
    LocalContainerEntityManagerFactoryBean entityManagerFactoryBean = new LocalContainerEntityManagerFactoryBean();
    entityManagerFactoryBean.setPersistenceUnitName("core");
    entityManagerFactoryBean.setPackagesToScan("org.obtima.core.**.model");
    entityManagerFactoryBean.setDataSource(dataSource());
    entityManagerFactoryBean.setJpaVendorAdapter(new HibernateJpaVendorAdapter());
    entityManagerFactoryBean.afterPropertiesSet();
    return entityManagerFactoryBean.getObject();
}

...but using Spring 4.3 GA and Hibernate 5.2 it leads to the following exception:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in org.obtima.core.config.ApplicationConfiguration: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [javax.persistence.EntityManagerFactory]: Factory method 'entityManagerFactory' threw exception; nested exception is java.lang.NoSuchMethodError: org.hibernate.boot.spi.SessionFactoryBuilderImplementor.markAsJpaBootstrap()V
	at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:599)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1123)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1018)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:510)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482)
	at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306)
	at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
	at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302)
	at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)
	at org.springframework.orm.jpa.EntityManagerFactoryUtils.findEntityManagerFactory(EntityManagerFactoryUtils.java:130)
	at org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor.findNamedEntityManagerFactory(PersistenceAnnotationBeanPostProcessor.java:561)
	at org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor.findEntityManagerFactory(PersistenceAnnotationBeanPostProcessor.java:543)
	at org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor$PersistenceElement.resolveEntityManagerFactory(PersistenceAnnotationBeanPostProcessor.java:698)
	at org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor$PersistenceElement.getResourceToInject(PersistenceAnnotationBeanPostProcessor.java:689)
	at org.springframework.beans.factory.annotation.InjectionMetadata$InjectedElement.inject(InjectionMetadata.java:169)
	at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88)
	at org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor.postProcessPropertyValues(PersistenceAnnotationBeanPostProcessor.java:354)
	... 58 more
Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [javax.persistence.EntityManagerFactory]: Factory method 'entityManagerFactory' threw exception; nested exception is java.lang.NoSuchMethodError: org.hibernate.boot.spi.SessionFactoryBuilderImplementor.markAsJpaBootstrap()V
	at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:189)
	at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:588)
	... 74 more
Caused by: java.lang.NoSuchMethodError: org.hibernate.boot.spi.SessionFactoryBuilderImplementor.markAsJpaBootstrap()V
	at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.populate(EntityManagerFactoryBuilderImpl.java:896)
	at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:885)
	at org.springframework.orm.jpa.vendor.SpringHibernateJpaPersistenceProvider.createContainerEntityManagerFactory(SpringHibernateJpaPersistenceProvider.java:60)
	at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.createNativeEntityManagerFactory(LocalContainerEntityManagerFactoryBean.java:338)
	at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.buildNativeEntityManagerFactory(AbstractEntityManagerFactoryBean.java:373)
	at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.afterPropertiesSet(AbstractEntityManagerFactoryBean.java:362)

@spring-projects-issues
Copy link
Collaborator Author

Juergen Hoeller commented

This looks like a conflict between old hibernate-entitymanager classes and new hibernate-core classes on your classpath. Please double-check that only hibernate-core in version 5.2 is on your classpath, and no variant of hibernate-entitymanager at all (since it's been merged into hibernate-core as of 5.2).

@spring-projects-issues
Copy link
Collaborator Author

Holger Stenzhorn commented

Please disregard my last comment: Things work as they should! The problem was that the old (obviously cached) Hibernate 5.1 JAR files were wrongly used instead of the Hibernate 5.2 JAR files. Sorry for the noise! :(

@spring-projects-issues
Copy link
Collaborator Author

Andrei Ivanov commented

5.2.1 is out, with hibernate-entitymanager as well :)

@spring-projects-issues
Copy link
Collaborator Author

Juergen Hoeller commented

Upgrading the Spring Framework 4.3.1 build to it. However, we'll keep not declaring hibernate-entitymanager in our build since it doesn't make a difference usage-wise.

It's nice for a seamless upgrade in Boot, of course! Makes the story much smoother there.

@spring-projects-issues
Copy link
Collaborator Author

Andrei Ivanov commented

Indeed, I see the pom of hibernate-entitymanager-5.2.1 mentions it is deprecated, so it's better not to use it.

@spring-projects-issues
Copy link
Collaborator Author

Ashok commented

Will there be a migration path to hibernate 5.* in spring 4.3?

@spring-projects-issues
Copy link
Collaborator Author

Ashok commented

Since Andrei Ivanov mentioned that hibernate-entitymanager-5.2.1 is deprecated (atleast as per its pom), will there be a migration path to hibernate 5.* in spring 4.3?

@spring-projects-issues
Copy link
Collaborator Author

Oliver Drotbohm commented

What do you mean with migration path? 5.2 shipped without a hibernate-entitymanager artifact as all code moved into hibernate-core. That made it hard for a lot of third party integrators (like Spring Boot) to consume the new artifacts and at the same time allow users to use old versions of Hibernate. For applications, this usually doesn't apply as there is a dedicated upgrade step. So upgrading your Spring application to Hibernate 5, you effectively need 3 steps:

  1. Upgrade to Spring 4.3.
  2. Change the Hibernate artifact version to 5.2(.1)
  3. Change the artifact identifier of Hibernate to hibernate-core.

There's nothing in Spring Framework code that needs to be tweaked.

@spring-projects-issues
Copy link
Collaborator Author

Ashok commented

Thanks for the clarification.

Earlier, I did not fully grasp what Andrei meant by this statement.
Indeed, I see the pom of hibernate-entitymanager-5.2.1 mentions it is deprecated, so it's better not to use it.

@spring-projects-issues
Copy link
Collaborator Author

Ashok commented

Now I get it.

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: enhancement A general enhancement
Projects
None yet
Development

No branches or pull requests

2 participants