Study/Java

[troubleshooting] Spring Boot 3.1.0에서 hibernate cannot be cast 오류 발생

Bluesky_ 2023. 6. 21. 01:53
반응형

Spring Boot 3.1.0으로 적용한 이후 특정 프로젝트가 startup 되지 않는 현상이 발견되었다.

발생한 오류는 다음과 같다.

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'someEntityManagerFactory' defined in class path resource [somepackage/config/SomeDataJpaConfig.class]: class org.hibernate.mapping.BasicValue cannot be cast to class org.hibernate.mapping.ToOne (org.hibernate.mapping.BasicValue and org.hibernate.mapping.ToOne are in unnamed module of loader 'app')
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1770)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:598)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:520)
        at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:326)
        at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234)
        at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:324)
        at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:200)
        at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1156)
        at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:931)
        at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:608)
        at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:146)
        at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:733)
        at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:435)
        at org.springframework.boot.SpringApplication.run(SpringApplication.java:311)
        at org.springframework.boot.SpringApplication.run(SpringApplication.java:1305)
        at org.springframework.boot.SpringApplication.run(SpringApplication.java:1294)
        at net.luversof.api.blog.Application.main(Application.java:10)
Caused by: java.lang.ClassCastException: class org.hibernate.mapping.BasicValue cannot be cast to class org.hibernate.mapping.ToOne (org.hibernate.mapping.BasicValue and org.hibernate.mapping.ToOne are in unnamed module of loader 'app')
        at org.hibernate.boot.model.internal.BinderHelper.checkMappedByType(BinderHelper.java:1086)
        at org.hibernate.boot.model.internal.CollectionBinder.isReversePropertyInJoin(CollectionBinder.java:1571)
        at org.hibernate.boot.model.internal.CollectionBinder.noAssociationTable(CollectionBinder.java:1582)
        at org.hibernate.boot.model.internal.CollectionBinder.bindStarToManySecondPass(CollectionBinder.java:1543)
        at org.hibernate.boot.model.internal.CollectionBinder$1.secondPass(CollectionBinder.java:1534)
        at org.hibernate.boot.model.internal.CollectionSecondPass.doSecondPass(CollectionSecondPass.java:45)
        at org.hibernate.boot.internal.InFlightMetadataCollectorImpl.processSecondPasses(InFlightMetadataCollectorImpl.java:1846)
        at org.hibernate.boot.internal.InFlightMetadataCollectorImpl.processSecondPasses(InFlightMetadataCollectorImpl.java:1803)
        at org.hibernate.boot.model.process.spi.MetadataBuildingProcess.complete(MetadataBuildingProcess.java:328)
        at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.metadata(EntityManagerFactoryBuilderImpl.java:1380)
        at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:1451)
        at org.springframework.orm.jpa.vendor.SpringHibernateJpaPersistenceProvider.createContainerEntityManagerFactory(SpringHibernateJpaPersistenceProvider.java:66)
        at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.createNativeEntityManagerFactory(LocalContainerEntityManagerFactoryBean.java:376)
        at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.buildNativeEntityManagerFactory(AbstractEntityManagerFactoryBean.java:409)
        at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.afterPropertiesSet(AbstractEntityManagerFactoryBean.java:396)
        at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.afterPropertiesSet(LocalContainerEntityManagerFactoryBean.java:352)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1816)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1766)
        ... 16 common frames omitted

발생한 에러 메시지는 class org.hibernate.mapping.BasicValue cannot be cast to class org.hibernate.mapping.ToOne 이다.

해당 오류는 서버 startup 시 DataJpaConfig 설정의 EntityManagerFactory 생성 시 참조하는 hibernate-core 6.2.2.Final에서 발생하며 Entity 간 관계 설정 시 양방향으로 관계 설정을 하지 않고 단방향으로 mappedBy로 지정을 했을 때 발생한다고 한다.

https://discourse.hibernate.org/t/spring-boot-3-with-hibernate-orm-6-2-0/7506

 

Spring Boot 3 with Hibernate ORM 6.2.0

I upgraded my Spring Boot Application to version 3.0.2 and use Hibernate ORM 6.1.7.Final Due to issues with “fetch” I wanted to try using ORM 6.2.0.Final to check if in this version our problems will go away but I even couldn’t start the app. I’m g

discourse.hibernate.org

 

관련해서 수정이 6.3.0에 되었다고 한다.

https://hibernate.atlassian.net/browse/HHH-16593

 

[HHH-16593] - Hibernate JIRA

회사에서 관리하는 프로젝트에 참여하고 있습니다

hibernate.atlassian.net

다만 이 글을 작성하는 시점에 hibernate ORM은 6.2.5.Final까지 release 되었고 6.3.0은 snapshot 버전이 올라가 있는 상태이다.

https://oss.sonatype.org/content/repositories/snapshots/org/hibernate/orm/hibernate-core/

 

Index of /repositories/snapshots/org/hibernate/orm/hibernate-core

6.1.6-SNAPSHOT/ Mon May 29 20:08:41 UTC 2023  

oss.sonatype.org

관련 이슈가 있는 경우 Spring Boot 3.1.0 적용을 늦추거나 Hibernate ORM 6.3.0 snapshot 반영이 문제없는지 확인이 필요할 듯하다.

반응형