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

Hibernate integration throws NoSuchMethodError for org.hibernate.cfg.AccessType in v9.0.0-M2 #951

Open
anil-subudhi opened this issue Jan 13, 2025 · 11 comments

Comments

@anil-subudhi
Copy link

Expected Behavior

Library: grails-datastore-gorm-hibernate5-9.0.0.pom

The type casting should be resolved from javax.persistence.AccessType to jakarta.persistence.AccessType while calling
'org.hibernate.cfg.AccessType org.hibernate.cfg.AccessType.getAccessStrategy(jakarta.persistence.AccessType)'

Actual Behaviour

Hibernate integration throws NoSuchMethodError

Caused by: java.lang.NoSuchMethodError: 'org.hibernate.cfg.AccessType org.hibernate.cfg.AccessType.getAccessStrategy(jakarta.persistence.AccessType)'
https://github.com/grails/gorm-hibernate5/blob/v9.0.0-M2/grails-datastore-gorm-hibernate/src/main/groovy/org/grails/orm/hibernate/cfg/GrailsDomainBinder.java#L2656

AccessType methods are below which doesn't contain overloaded method for org.hibernate.cfg.AccessType.getAccessStrategy(jakarta.persistence.AccessType)

This leads to failure with NoSuchMethodError

AccessType methods:

[public static org.hibernate.cfg.AccessType[] org.hibernate.cfg.AccessType.values(), public static org.hibernate.cfg.AccessType org.hibernate.cfg.AccessType.valueOf(java.lang.String), public java.lang.String org.hibernate.cfg.AccessType.getType(), public static org.hibernate.cfg.AccessType org.hibernate.cfg.AccessType.getAccessStrategy(java.lang.String), public static org.hibernate.cfg.AccessType org.hibernate.cfg.AccessType.getAccessStrategy(javax.persistence.AccessType), public final java.lang.String java.lang.Enum.name(), public final boolean java.lang.Enum.equals(java.lang.Object), public java.lang.String java.lang.Enum.toString(), public final int java.lang.Enum.hashCode(), public int java.lang.Enum.compareTo(java.lang.Object), public final int java.lang.Enum.compareTo(java.lang.Enum), public static java.lang.Enum java.lang.Enum.valueOf(java.lang.Class,java.lang.String), public final java.util.Optional java.lang.Enum.describeConstable(), public final java.lang.Class java.lang.Enum.getDeclaringClass(), public final int java.lang.Enum.ordinal(), public final void java.lang.Object.wait(long,int) throws java.lang.InterruptedException, public final void java.lang.Object.wait() throws java.lang.InterruptedException, public final native void java.lang.Object.wait(long) throws java.lang.InterruptedException, public final native java.lang.Class java.lang.Object.getClass(), public final native void java.lang.Object.notify(), public final native void java.lang.Object.notifyAll()]

Caused by: java.lang.NoSuchMethodError: 'org.hibernate.cfg.AccessType org.hibernate.cfg.AccessType.getAccessStrategy(jakarta.persistence.AccessType)'
at org.grails.orm.hibernate.cfg.GrailsDomainBinder.bindProperty(GrailsDomainBinder.java:2656)
at org.grails.orm.hibernate.cfg.GrailsDomainBinder.createProperty(GrailsDomainBinder.java:2313)
at org.grails.orm.hibernate.cfg.GrailsDomainBinder.bindComponentProperty(GrailsDomainBinder.java:2281)
at org.grails.orm.hibernate.cfg.GrailsDomainBinder.bindCompositeId(GrailsDomainBinder.java:1854)
at org.grails.orm.hibernate.cfg.GrailsDomainBinder.bindIdentity(GrailsDomainBinder.java:1814)
at org.grails.orm.hibernate.cfg.GrailsDomainBinder.bindRootPersistentClassCommonValues(GrailsDomainBinder.java:1776)
at org.grails.orm.hibernate.cfg.GrailsDomainBinder.bindRoot(GrailsDomainBinder.java:1401)
at org.grails.orm.hibernate.cfg.GrailsDomainBinder.contribute(GrailsDomainBinder.java:161)
at org.hibernate.boot.model.process.spi.MetadataBuildingProcess.complete(MetadataBuildingProcess.java:292)
at org.hibernate.boot.model.process.spi.MetadataBuildingProcess.build(MetadataBuildingProcess.java:86)
at org.hibernate.boot.internal.MetadataBuilderImpl.build(MetadataBuilderImpl.java:479)
at org.hibernate.boot.internal.MetadataBuilderImpl.build(MetadataBuilderImpl.java:85)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:709)
at org.grails.orm.hibernate.cfg.HibernateMappingContextConfiguration.buildSessionFactory(HibernateMappingContextConfiguration.java:287)
at org.grails.orm.hibernate.connections.HibernateConnectionSourceFactory.create(HibernateConnectionSourceFactory.java:86)
at org.grails.orm.hibernate.connections.AbstractHibernateConnectionSourceFactory.create(AbstractHibernateConnectionSourceFactory.java:39)
at org.grails.orm.hibernate.connections.AbstractHibernateConnectionSourceFactory.create(AbstractHibernateConnectionSourceFactory.java:23)
at org.grails.datastore.mapping.core.connections.AbstractConnectionSourceFactory.create(AbstractConnectionSourceFactory.java:64)
at org.grails.datastore.mapping.core.connections.AbstractConnectionSourceFactory.create(AbstractConnectionSourceFactory.java:52)
at org.grails.datastore.mapping.core.connections.ConnectionSourcesInitializer.create(ConnectionSourcesInitializer.groovy:24)
at org.grails.orm.hibernate.HibernateDatastore.(HibernateDatastore.java:212)
at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:77)
at java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.base/java.lang.reflect.Constructor.newInstanceWithCaller(Constructor.java:499)
at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:480)
at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:209)

Steps To Reproduce

  1. Create the grails 7.0.0-M1 app
  2. Enable hibernate feature
  3. Follow the GORM steps to add domain https://docs.grails.org/7.0.0-M1/guide/single.html#whatsNew
  4. Application start ./gradlew bootRun

Environment Information

| Grails Version: 7.0.0-M1
| JVM Version: 17.0.9

Gradle 8.12

OS: Mac OS X 15.1.1 x86_64

Example Application

No response

Version

7.0.0-M1

@jamesfredley
Copy link
Contributor

Grails 7 requires migrating everything javax.* to jakarta.* to work with Spring 6.2 and Spring Boot 3.4. You will need to make the same changes in your application.

https://github.com/grails/grails-core/blob/7.0.x/UPGRADE7.md lists this and many other breaking changes that were necessary to move to the latest and only OSS supported version of Spring and Spring Boot.

@jeffscottbrown
Copy link
Member

You will need to make the same changes in your application.

@anil-subudhi is the problematic app described in the original post one that was created with 7.0.0-M1 or one that was upgraded?

@anil-subudhi
Copy link
Author

anil-subudhi commented Jan 15, 2025

@jeffscottbrown , I am facing on upgrade to Grails 7.0.0-M1, the build contains below hibernate depedencies

grails-datastore-gorm-hibernate5-9.0.0-M2.jar
hibernate-commons-annotations-5.1.2.Final.jar
hibernate-core-5.6.15.Final.jar
hibernate-core-jakarta-5.6.15.Final.jar
hibernate-ehcache-5.6.15.Final.jar
hibernate-validator-8.0.2.Final.jar
hibernate5-9.0.0-M2.jar

When tried to get list of method of org.hibernate.cfg.AccessType, I don't see any such overloaded methods with jakarta.persistence.AccessType , to resolve this I modified grails-datastore-gorm-hibernate5-9.0.0-M2 and converted to string

    AccessType accessType = AccessType.getAccessStrategy(
            grailsProperty.getMapping().getMappedForm().getAccessType().**toString()**
    );

org.hibernate.cfg.AccessType methods:
[public static org.hibernate.cfg.AccessType[] org.hibernate.cfg.AccessType.values(), public static org.hibernate.cfg.AccessType org.hibernate.cfg.AccessType.valueOf(java.lang.String), public java.lang.String org.hibernate.cfg.AccessType.getType(), public static org.hibernate.cfg.AccessType org.hibernate.cfg.AccessType.getAccessStrategy(java.lang.String), public static org.hibernate.cfg.AccessType org.hibernate.cfg.AccessType.getAccessStrategy(javax.persistence.AccessType), public final java.lang.String java.lang.Enum.name(), public final boolean java.lang.Enum.equals(java.lang.Object), public java.lang.String java.lang.Enum.toString(), public final int java.lang.Enum.hashCode(), public int java.lang.Enum.compareTo(java.lang.Object), public final int java.lang.Enum.compareTo(java.lang.Enum), public static java.lang.Enum java.lang.Enum.valueOf(java.lang.Class,java.lang.String), public final java.util.Optional java.lang.Enum.describeConstable(), public final java.lang.Class java.lang.Enum.getDeclaringClass(), public final int java.lang.Enum.ordinal(), public final void java.lang.Object.wait(long,int) throws java.lang.InterruptedException, public final void java.lang.Object.wait() throws java.lang.InterruptedException, public final native void java.lang.Object.wait(long) throws java.lang.InterruptedException, public final native java.lang.Class java.lang.Object.getClass(), public final native void java.lang.Object.notify(), public final native void java.lang.Object.notifyAll()]

@jeffscottbrown
Copy link
Member

I am facing on upgrade to Grails 7.0.0-M1

That is good info. That wasn't clear to me reading through the steps to reproduce, and that is helpful information. Thank you for the clarification!

@lynchie14
Copy link

hibernate-ehcache is pulling in the non jakarta hibernate-core-5.6.15.Final.jar artifact. Is that causing the issue with duplicate classes on the classpath? I assume only one of the hibernate core libraries should be included?

@anil-subudhi
Copy link
Author

anil-subudhi commented Jan 17, 2025

Dependence tree:

**+--- org.grails.plugins:hibernate5 -> 9.0.0-M2**
|    +--- org.springframework.boot:spring-boot -> 3.4.1 (*)
|    +--- org.springframework:spring-orm -> 6.2.1
|    |    +--- org.springframework:spring-beans:6.2.1 (*)
|    |    +--- org.springframework:spring-core:6.2.1 (*)
|    |    +--- org.springframework:spring-jdbc:6.2.1 (*)
|    |    \--- org.springframework:spring-tx:6.2.1 (*)
|    **+--- org.hibernate:hibernate-core-jakarta:5.6.15.Final**
|    |    +--- org.jboss.logging:jboss-logging:3.4.3.Final -> 3.6.1.Final
|    |    +--- jakarta.persistence:jakarta.persistence-api:3.0.0 -> 3.1.0
|    |    +--- net.bytebuddy:byte-buddy:1.12.18 -> 1.15.5
|    |    +--- antlr:antlr:2.7.7
|    |    +--- jakarta.transaction:jakarta.transaction-api:2.0.0 -> 2.0.1
|    |    +--- org.jboss:jandex:2.4.2.Final
|    |    +--- com.fasterxml:classmate:1.5.1 -> 1.7.0
|    |    +--- jakarta.activation:jakarta.activation-api:2.0.1 -> 2.1.3
|    |    +--- org.dom4j:dom4j:2.1.3
|    |    +--- org.hibernate.common:hibernate-commons-annotations:5.1.2.Final
|    |    |    \--- org.jboss.logging:jboss-logging:3.3.2.Final -> 3.6.1.Final
|    |    +--- jakarta.xml.bind:jakarta.xml.bind-api:3.0.1 -> 4.0.2 (*)
|    |    \--- org.glassfish.jaxb:jaxb-runtime:3.0.0 -> 4.0.5
|    |         \--- org.glassfish.jaxb:jaxb-core:4.0.5
|    |              +--- jakarta.xml.bind:jakarta.xml.bind-api:4.0.2 (*)
|    |              +--- jakarta.activation:jakarta.activation-api:2.1.3
|    |              +--- org.glassfish.jaxb:txw2:4.0.5
|    |              \--- com.sun.istack:istack-commons-runtime:4.1.2
|    **+--- org.hibernate:hibernate-ehcache:5.6.15.Final**
|    |    +--- org.jboss.logging:jboss-logging:3.4.3.Final -> 3.6.1.Final
|    |    **+--- org.hibernate:hibernate-core:5.6.15.Final**

@jeffscottbrown
Copy link
Member

In a newly created 7.0.0-M1 app:

+--- org.grails.plugins:hibernate5 -> 9.0.0-M2
|    +--- org.springframework.boot:spring-boot -> 3.4.1 (*)
|    +--- org.springframework:spring-orm -> 6.2.1
|    |    +--- org.springframework:spring-beans:6.2.1 (*)
|    |    +--- org.springframework:spring-core:6.2.1 (*)
|    |    +--- org.springframework:spring-jdbc:6.2.1 (*)
|    |    \--- org.springframework:spring-tx:6.2.1 (*)
|    +--- org.hibernate:hibernate-core-jakarta:5.6.15.Final
|    |    +--- org.jboss.logging:jboss-logging:3.4.3.Final -> 3.6.1.Final
|    |    +--- jakarta.persistence:jakarta.persistence-api:3.0.0 -> 3.1.0
|    |    +--- net.bytebuddy:byte-buddy:1.12.18 -> 1.15.5
|    |    +--- antlr:antlr:2.7.7
|    |    +--- jakarta.transaction:jakarta.transaction-api:2.0.0 -> 2.0.1
|    |    +--- org.jboss:jandex:2.4.2.Final
|    |    +--- com.fasterxml:classmate:1.5.1 -> 1.7.0
|    |    +--- jakarta.activation:jakarta.activation-api:2.0.1 -> 2.1.3
|    |    +--- org.dom4j:dom4j:2.1.3
|    |    +--- org.hibernate.common:hibernate-commons-annotations:5.1.2.Final
|    |    |    \--- org.jboss.logging:jboss-logging:3.3.2.Final -> 3.6.1.Final
|    |    +--- jakarta.xml.bind:jakarta.xml.bind-api:3.0.1 -> 4.0.2 (*)
|    |    \--- org.glassfish.jaxb:jaxb-runtime:3.0.0 -> 4.0.5
|    |         \--- org.glassfish.jaxb:jaxb-core:4.0.5
|    |              +--- jakarta.xml.bind:jakarta.xml.bind-api:4.0.2 (*)
|    |              +--- jakarta.activation:jakarta.activation-api:2.1.3
|    |              +--- org.glassfish.jaxb:txw2:4.0.5
|    |              \--- com.sun.istack:istack-commons-runtime:4.1.2
|    +--- org.hibernate:hibernate-ehcache:5.6.15.Final
|    |    +--- org.jboss.logging:jboss-logging:3.4.3.Final -> 3.6.1.Final
|    |    +--- org.hibernate:hibernate-core:5.6.15.Final
|    |    |    +--- org.jboss.logging:jboss-logging:3.4.3.Final -> 3.6.1.Final
|    |    |    +--- javax.persistence:javax.persistence-api:2.2
|    |    |    +--- net.bytebuddy:byte-buddy:1.12.18 -> 1.15.5
|    |    |    +--- antlr:antlr:2.7.7
|    |    |    +--- org.jboss.spec.javax.transaction:jboss-transaction-api_1.2_spec:1.1.1.Final
|    |    |    +--- org.jboss:jandex:2.4.2.Final
|    |    |    +--- com.fasterxml:classmate:1.5.1 -> 1.7.0
|    |    |    +--- javax.activation:javax.activation-api:1.2.0
|    |    |    +--- org.hibernate.common:hibernate-commons-annotations:5.1.2.Final (*)
|    |    |    +--- javax.xml.bind:jaxb-api:2.3.1
|    |    |    |    \--- javax.activation:javax.activation-api:1.2.0
|    |    |    \--- org.glassfish.jaxb:jaxb-runtime:2.3.1 -> 4.0.5 (*)
|    |    \--- net.sf.ehcache:ehcache:2.10.6
|    |         \--- org.slf4j:slf4j-api:1.7.25 -> 2.0.16

@jamesfredley
Copy link
Contributor

jamesfredley commented Jan 17, 2025

I believe we will need to exclude org.hibernate:hibernate-core from org.hibernate:hibernate-ehcache. org.hibernate:hibernate-ehcache appears to be compatible with org.hibernate:hibernate-core-jakarta:5.6.15.Final

@jamesfredley jamesfredley transferred this issue from grails/grails-core Jan 17, 2025
@jamesfredley jamesfredley moved this to Todo in Grails 7 Jan 17, 2025
@anil-subudhi
Copy link
Author

Reference to another issue #952

@thatisuresh
Copy link

thatisuresh commented Jan 30, 2025

Hi @anil-subudhi
I am trying to upgrade to grails 7.0.0-M1 and encountered the same exception that you mentioned above. I tried to follow the workaround you proposed where I added toString() but I am running into the below exception. Wondering if I miss anything else here. Not sure why it looks for javax validation instead of jakarta. Your help is appreciated.

Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'hibernateDatastore': Failed to instantiate [org.grails.orm.hibernate.HibernateDatastore]: Constructor threw exception
at org.springframework.beans.factory.support.ConstructorResolver.instantiate(ConstructorResolver.java:321)
at org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:309)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireConstructor(AbstractAutowireCapableBeanFactory.java:1377)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1214)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:563)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:523)
at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:336)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:289)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:334)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:199)
at org.springframework.beans.factory.config.DependencyDescriptor.resolveCandidate(DependencyDescriptor.java:254)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.addCandidateEntry(DefaultListableBeanFactory.java:1883)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.findAutowireCandidates(DefaultListableBeanFactory.java:1847)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveMultipleBeans(DefaultListableBeanFactory.java:1686)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1580)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1519)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredMethodElement.resolveMethodArguments(AutowiredAnnotationBeanPostProcessor.java:888)
... 111 more
Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.grails.orm.hibernate.HibernateDatastore]: Constructor threw exception
at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:222)
at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:145)
at org.springframework.beans.factory.support.ConstructorResolver.instantiate(ConstructorResolver.java:318)
... 127 more
Caused by: java.lang.NoClassDefFoundError: javax/validation/ValidatorFactory
at java.base/java.lang.Class.getDeclaredMethods0(Native Method)
at java.base/java.lang.Class.privateGetDeclaredMethods(Class.java:3402)
at java.base/java.lang.Class.getMethodsRecursive(Class.java:3543)
at java.base/java.lang.Class.getMethod0(Class.java:3529)
at java.base/java.lang.Class.getMethod(Class.java:2225)
at org.hibernate.cfg.beanvalidation.BeanValidationIntegrator.integrate(BeanValidationIntegrator.java:113)
at org.hibernate.internal.SessionFactoryImpl.(SessionFactoryImpl.java:300)
at org.hibernate.boot.internal.SessionFactoryBuilderImpl.build(SessionFactoryBuilderImpl.java:471)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:728)
at org.grails.orm.hibernate.cfg.HibernateMappingContextConfiguration.buildSessionFactory(HibernateMappingContextConfiguration.java:287)
at org.grails.orm.hibernate.connections.HibernateConnectionSourceFactory.create(HibernateConnectionSourceFactory.java:86)
at org.grails.orm.hibernate.connections.AbstractHibernateConnectionSourceFactory.create(AbstractHibernateConnectionSourceFactory.java:39)
at org.grails.orm.hibernate.connections.AbstractHibernateConnectionSourceFactory.create(AbstractHibernateConnectionSourceFactory.java:23)
at org.grails.datastore.mapping.core.connections.AbstractConnectionSourceFactory.create(AbstractConnectionSourceFactory.java:64)
at org.grails.datastore.mapping.core.connections.AbstractConnectionSourceFactory.create(AbstractConnectionSourceFactory.java:52)
at org.grails.datastore.mapping.core.connections.ConnectionSourcesInitializer.create(ConnectionSourcesInitializer.groovy:24)
at org.grails.orm.hibernate.HibernateDatastore.(HibernateDatastore.java:212)
at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:77)
at java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.base/java.lang.reflect.Constructor.newInstanceWithCaller(Constructor.java:500)
at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:481)
at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:209)
... 129 more
Caused by: java.lang.ClassNotFoundException: javax.validation.ValidatorFactory
at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1222)
at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1053)
... 152 more

@lynchie14
Copy link

You still have the original hibernate on your classpath.

Try adding

configurations {
all {
exclude group: 'org.hibernate', module: 'hibernate-core'
}
}

to your build.gradle

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: Todo
Development

No branches or pull requests

5 participants