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-issuemaster opened this issue Jun 2, 2016 · 18 comments

Comments

@spring-issuemaster
Copy link
Collaborator

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:

  • #18906 Add support for Hibernate 5.2 ("is duplicated by")
  • #18059 Compatibility with Hibernate ORM 5.1
  • #19897 Hibernate Envers listeners fail because EntityManager is closed too early when using JPA/JTA/Hibernate 5.2.8/Envers
  • #18937 NoSuchMethodError for SessionFactoryImplementor.getProperties() with Hibernate 5.1
  • #18966 JDBC Connection to reset not identical to originally prepared Connection with Hibernate 5.2
  • #18996 HibernateTemplate not binary-compatible with Hibernate 5.0/5.1's Query type
  • #20850 Support for Hibernate ORM 5.3
  • #12206 findDefaultEntityManagerFactory should consider EMF bean's primary flag
  • #19024 Hibernate5 LocalSessionFactoryBean does not translate javax.persistence exceptions
  • #19577 Scoped EntityManager bean cannot get unwrapped to Session (with Hibernate 5.2)

0 votes, 11 watchers

@spring-issuemaster

This comment has been minimized.

Copy link
Collaborator Author

commented Jun 2, 2016

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-issuemaster

This comment has been minimized.

Copy link
Collaborator Author

commented Jun 3, 2016

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-issuemaster

This comment has been minimized.

Copy link
Collaborator Author

commented Jun 3, 2016

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-issuemaster

This comment has been minimized.

Copy link
Collaborator Author

commented Jun 5, 2016

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-issuemaster

This comment has been minimized.

Copy link
Collaborator Author

commented Jun 5, 2016

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-issuemaster

This comment has been minimized.

Copy link
Collaborator Author

commented Jun 5, 2016

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-issuemaster

This comment has been minimized.

Copy link
Collaborator Author

commented Jun 5, 2016

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-issuemaster

This comment has been minimized.

Copy link
Collaborator Author

commented Jun 10, 2016

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-issuemaster

This comment has been minimized.

Copy link
Collaborator Author

commented Jun 10, 2016

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-issuemaster

This comment has been minimized.

Copy link
Collaborator Author

commented Jun 10, 2016

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-issuemaster

This comment has been minimized.

Copy link
Collaborator Author

commented Jul 1, 2016

Andrei Ivanov commented

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

@spring-issuemaster

This comment has been minimized.

Copy link
Collaborator Author

commented Jul 1, 2016

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-issuemaster

This comment has been minimized.

Copy link
Collaborator Author

commented Jul 1, 2016

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-issuemaster

This comment has been minimized.

Copy link
Collaborator Author

commented Jul 12, 2016

Ashok commented

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

@spring-issuemaster

This comment has been minimized.

Copy link
Collaborator Author

commented Jul 12, 2016

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-issuemaster

This comment has been minimized.

Copy link
Collaborator Author

commented Jul 12, 2016

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-issuemaster

This comment has been minimized.

Copy link
Collaborator Author

commented Jul 13, 2016

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-issuemaster

This comment has been minimized.

Copy link
Collaborator Author

commented Jul 13, 2016

Ashok commented

Now I get it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
2 participants
You can’t perform that action at this time.