NoSuchMethodError in org.hibernate.ogm.boot.impl.OgmSessionFactoryBuilderImpl.build

Description

Trying to create an EntityManager programatically in a Spring Boot environment with the goal to configure Hibernate OGM with Spring properties.

The properties that were addressed to configure hibernate are:

 

During Integration Test (mvn -X -B verify) with maven failsafe plugin (3.0.0-M4) a BeanInstantiationException with the cause
Caused by: java.lang.NoSuchMethodError: 'void org.hibernate.internal.SessionFactoryImpl.<init>(org.hibernate.boot.spi.BootstrapContext, org.hibernate.boot.spi.MetadataImplementor, org.hibernate.boot.spi.SessionFactoryOptions)'
at org.hibernate.ogm.boot.impl.OgmSessionFactoryBuilderImpl.build(OgmSessionFactoryBuilderImpl.java:56) ~[hibernate-ogm-core-5.4.1.Final.jar:5.4.1.Final]
at org.hibernate.ogm.boot.impl.OgmSessionFactoryBuilderImpl.build(OgmSessionFactoryBuilderImpl.java:23) ~[hibernate-ogm-core-5.4.1.Final.jar:5.4.1.Final]
at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:938) ~[hibernate-core-5.4.1.Final.jar:5.4.1.Final]
at org.hibernate.jpa.HibernatePersistenceProvider.createContainerEntityManagerFactory(HibernatePersistenceProvider.java:141) ~[hibernate-core-5.4.1.Final.jar:5.4.1.Final]
at org.hibernate.ogm.jpa.HibernateOgmPersistence.createContainerEntityManagerFactory(HibernateOgmPersistence.java:89) ~[hibernate-ogm-core-5.4.1.Final.jar:5.4.1.Final]
at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.createNativeEntityManagerFactory(LocalContainerEntityManagerFactoryBean.java:365) ~[spring-orm-5.2.9.RELEASE.jar:5.2.9.RELEASE]
at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.buildNativeEntityManagerFactory(AbstractEntityManagerFactoryBean.java:391) ~[spring-orm-5.2.9.RELEASE.jar:5.2.9.RELEASE]
at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.afterPropertiesSet(AbstractEntityManagerFactoryBean.java:378) ~[spring-orm-5.2.9.RELEASE.jar:5.2.9.RELEASE]
at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.afterPropertiesSet(LocalContainerEntityManagerFactoryBean.java:341) ~[spring-orm-5.2.9.RELEASE.jar:5.2.9.RELEASE]
at com.amy.HibernateOgmEmfProvider.entityManager(HibernateOgmEmfProvider.java:74) ~[classes/:na]
at com.amy.HibernateOgmEmfProvider$$EnhancerBySpringCGLIB$$372c3d69.CGLIB$entityManager$1(<generated>) ~[classes/:na]
at com.amy.HibernateOgmEmfProvider$$EnhancerBySpringCGLIB$$372c3d69$$FastClassBySpringCGLIB$$bd4561a4.invoke(<generated>) ~[classes/:na]
at org.springframework.cglib.proxy.MethodProxy.invokeSuper(MethodProxy.java:244) ~[spring-core-5.2.9.RELEASE.jar:5.2.9.RELEASE]
at org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor.intercept(ConfigurationClassEnhancer.java:331) ~[spring-context-5.2.9.RELEASE.jar:5.2.9.RELEASE]
at com.amy.HibernateOgmEmfProvider$$EnhancerBySpringCGLIB$$372c3d69.entityManager(<generated>) ~[classes/:na]
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:na]
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:na]
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:na]
at java.base/java.lang.reflect.Method.invoke(Method.java:566) ~[na:na]
at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:154) ~[spring-beans-5.2.9.RELEASE.jar:5.2.9.RELEASE]
... 100 common frames omitted

The signature of the constructor of org.hibernate.internal.SessionFactoryImpl in hibernate-orm is
SessionFactoryImpl(
final MetadataImplementor metadata,
SessionFactoryOptions options,
QueryPlanCache.QueryPlanCreator queryPlanCacheFunction)

but in line 56 in org.hibernate.ogm.boot.impl.OgmSessionFactoryBuilderImpl it's being called by
new SessionFactoryImpl( null, metadata, options )

I’m only a user of hibernate-ogm and don’t know how to write a test case for this issue. I build hibernate-ogm by myself and it passes. When I twisted the attributes in line 56 to
new SessionFactoryImpl(metadata, options, null)
the build fails with OgmSessionFactoryBuilderImpl.java:[56,75] incompatible types: org.hibernate.boot.spi.MetadataImplementor cannot be converted to org.hibernate.boot.spi.BootstrapContext
Is there a problem with packaging different versions of hibernate-orm? For checking this, I added my pom.

However in my environment the MongoDB server is running, db exists, network connection is up and auth credentials are correct. The only problem for me is how the constructor of SessionFactoryImpl is being called. That always throws the NoSuchMethodError during integration tests, when Spring Boot creates its Context and tries to create an EntityManager .

The mongodb driver itself is able to connect:

org.mongodb.driver.cluster : Checking status of 127.0.0.1:27017
org.mongodb.driver.protocol.command : Sending command '{ "ismaster" : 1, "$db" : "admin" }' with request id 173 to database admin on connection [connectionId{localValue:11, serverValue:11}] to server 127.0.0.1:27017
org.mongodb.driver.protocol.command : Execution of command with request id 173 completed successfully in 1.62 ms on connection [connectionId{localValue:11, serverValue:11}] to server 127.0.0.1:27017
org.mongodb.driver.cluster : Updating cluster description to {type=STANDALONE, servers=[{address=127.0.0.1:27017, type=STANDALONE, roundTripTime=9.3 ms, state=CONNECTED}]
org.mongodb.driver.cluster : Checking status of 127.0.0.1:27017
org.mongodb.driver.protocol.command : Sending command '{ "ismaster" : 1, "$db" : "admin" }' with request id 174 to database admin on connection [connectionId{localValue:5, serverValue:5}] to server 127.0.0.1:27017
org.mongodb.driver.protocol.command : Execution of command with request id 174 completed successfully in 2.90 ms on connection [connectionId{localValue:5, serverValue:5}] to server 127.0.0.1:27017
org.mongodb.driver.cluster : Updating cluster description to {type=STANDALONE, servers=[{address=127.0.0.1:27017, type=STANDALONE, roundTripTime=26.5 ms, state=CONNECTED}]

 

Environment

hibernate-core, hibernate-ogm-core and hibernate-ogm-mongodb (5.4.1) (plus mongo-java-driver and bson) included in maven spring boot project (2.3.4.RELEASE)
build by Jenkins using Maven 3.6.0 and Java 11.0.8 (java-11-openjdk-amd64)
on Debian GNU/Linux 10 (buster)
failed integration test using maven-failsafe-plugin 3.0.0-M4

Activity

Show:
Yoann Rodière
October 8, 2020, 6:28 AM
Edited

Thanks for your report, but this is not a bug: it's an issue with your dependencies.

As explained on this page, Hibernate OGM 5.4 requires Hibernate ORM 5.3.
Spring Boot 2.3.4.RELEASE relies on Hibernate ORM 5.4.

In order to use Hibernate OGM with Spring Boot 2.3.4.RELEASE, you need to override the version of Hibernate ORM.

I would first start by applying Hibernate OGM's BOM, as explained in this section of the documentation.

Alternatively, if the BOM doesn't override Spring Boot's dependency manangement for some reason, you can set this property in your POM:

Please confirm that this solves your problem so that I can close this ticket.

Alexander Stumpf
October 13, 2020, 6:14 PM
Edited

Thanks for your answer and sorry for my late reply.
Directly after reading your message I downgraded/overrode the dependency of hibernate-core to version 5.3.6.Final but that replaced the NoSuchMethodException with an IllegalArgumentException.
I searched deeper for possible reasons of the problem and found out that due to the bad API-management of hibernate-core, it has erratic changes from version 5.3 to 5.4. This makes hibernate-core neither forward nor backward compatible which makes all frameworks which use the different versions also incompatible. So I downgraded my hole project to Spring Boot Starter 2.1 but that had no effect. I still got the same Exception. Meanwhile I got a workaround by implementing javax.persistence.spi.PersistenceUnitInfo. But org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean can't be used for that use case no matter what release of which framework I use. Now I don't known if it is an issue with Hibernate or Spring Boot.

Stacktrace:
Caused by: java.lang.IllegalArgumentException: methods with same signature getSessionFactory() but incompatible return types: [interface org.hibernate.ogm.OgmSessionFactory, interface org.hibernate.engine.spi.SessionFactoryImplementor]
at java.base/java.lang.reflect.ProxyGenerator.checkReturnTypes(ProxyGenerator.java:713) ~[na:na]
at java.base/java.lang.reflect.ProxyGenerator.generateClassFile(ProxyGenerator.java:462) ~[na:na]
at java.base/java.lang.reflect.ProxyGenerator.generateProxyClass(ProxyGenerator.java:338) ~[na:na]
at java.base/java.lang.reflect.Proxy$ProxyBuilder.defineProxyClass(Proxy.java:535) ~[na:na]
at java.base/java.lang.reflect.Proxy$ProxyBuilder.build(Proxy.java:648) ~[na:na]
at java.base/java.lang.reflect.Proxy.lambda$getProxyConstructor$1(Proxy.java:426) ~[na:na]
at java.base/jdk.internal.loader.AbstractClassLoaderValue$Memoizer.get(AbstractClassLoaderValue.java:329) ~[na:na]
at java.base/jdk.internal.loader.AbstractClassLoaderValue.computeIfAbsent(AbstractClassLoaderValue.java:205) ~[na:na]
at java.base/java.lang.reflect.Proxy.getProxyConstructor(Proxy.java:424) ~[na:na]
at java.base/java.lang.reflect.Proxy.newProxyInstance(Proxy.java:1006) ~[na:na]
at org.springframework.orm.jpa.ExtendedEntityManagerCreator.createProxy(ExtendedEntityManagerCreator.java:233) ~[spring-orm-5.1.2.RELEASE.jar:5.1.2.RELEASE]
at org.springframework.orm.jpa.ExtendedEntityManagerCreator.createProxy(ExtendedEntityManagerCreator.java:200) ~[spring-orm-5.1.2.RELEASE.jar:5.1.2.RELEASE]
at org.springframework.orm.jpa.ExtendedEntityManagerCreator.createApplicationManagedEntityManager(ExtendedEntityManagerCreator.java:104) ~[spring-orm-5.1.2.RELEASE.jar:5.1.2.RELEASE]
at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.invokeProxyMethod(AbstractEntityManagerFactoryBean.java:500) ~[spring-orm-5.1.2.RELEASE.jar:5.1.2.RELEASE]
at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean$ManagedEntityManagerFactoryInvocationHandler.invoke(AbstractEntityManagerFactoryBean.java:679) ~[spring-orm-5.1.2.RELEASE.jar:5.1.2.RELEASE]
at com.sun.proxy.$Proxy142.createEntityManager(Unknown Source) ~[na:na]
at com.amy.HibernateOgmEmfProvider.entityManager(HibernateOgmEmfProvider.java:100) ~[classes/:na]
at com.amy.HibernateOgmEmfProvider$$EnhancerBySpringCGLIB$$c0933a7c.CGLIB$entityManager$2(<generated>) ~[classes/:na]
at com.amy.HibernateOgmEmfProvider$$EnhancerBySpringCGLIB$$c0933a7c$$FastClassBySpringCGLIB$$ce7fa5dc.invoke(<generated>) ~[classes/:na]
at org.springframework.cglib.proxy.MethodProxy.invokeSuper(MethodProxy.java:244) ~[spring-core-5.1.2.RELEASE.jar:5.1.2.RELEASE]
at org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor.intercept(ConfigurationClassEnhancer.java:363) ~[spring-context-5.1.2.RELEASE.jar:5.1.2.RELEASE]
at com.amy.HibernateOgmEmfProvider$$EnhancerBySpringCGLIB$$c0933a7c.entityManager(<generated>) ~[classes/:na]
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:na]
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:na]
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:na]
at java.base/java.lang.reflect.Method.invoke(Method.java:566) ~[na:na]
at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:154) ~[spring-beans-5.1.2.RELEASE.jar:5.1.2.RELEASE]
... 60 common frames omitted

This exception is always thrown at the last line of the described method HibernateOgmEmfProvider#entityManager().

Assignee

Unassigned

Reporter

Alexander Stumpf

Feedback Requested

None

Feedback Requested By

None

backPortable

None

Suitable for new contributors

None

Pull Request

None

backportDecision

None

backportReEvaluate

None

Components

Affects versions

Priority

Blocker
Configure