简体   繁体   English

Spring Boot - 带有 Kotlin 列表的实体<enum>不开机</enum>

[英]Spring Boot - Entity With Kotlin List<Enum> Not Booting

I have been helping my team upgrade our Maven/SpringBoot/Kotlin project from Spring-Boot 2.7.5 to Spring-Boot 3.0.0.我一直在帮助我的团队将我们的 Maven/SpringBoot/Kotlin 项目从 Spring-Boot 2.7.5 升级到 Spring-Boot 3.0.0。 However, an issue on startup is preventing us from progressing.然而,启动时的一个问题阻碍了我们的进步。 This has not been an issue before Spring-Boot 3.0.0.在 Spring-Boot 3.0.0 之前,这不是问题。

Upon booting the application, I receive the following stack trace:启动应用程序后,我收到以下堆栈跟踪:

org.springframework.context.ApplicationContextException: Unable to start web server
...
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'jpaSharedEM_entityManagerFactory': Cannot resolve reference to bean 'entityManagerFactory' while setting constructor argument
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaConfiguration.class]: class sun.reflect.generics.reflectiveObjects.WildcardTypeImpl cannot be cast to class java.lang.reflect.ParameterizedType (sun.reflect.generics.reflectiveObjects.WildcardTypeImpl and java.lang.reflect.ParameterizedType are in module java.base of loader 'bootstrap')

After spending a day checking configurations and isolating the problem, we were left with one entity in our application, where we were still having the issue.在花了一天时间检查配置并隔离问题后,我们的应用程序中只剩下一个实体,我们仍然遇到问题。 We then started removing fields from the entity, until the application was able to run.然后我们开始从实体中删除字段,直到应用程序能够运行。 The field we removed was a kotlin.collections.List of type Interaction , an enum class that we had defined for the application.我们删除的字段是Interaction类型的kotlin.collections.List ,这是我们为应用程序定义的枚举类。

In order to ensure privacy, here is an isolated slice of the application MVC that will replicate this issue:为了确保隐私,这里是将复制此问题的应用程序 MVC 的一个独立片段:

package com.example.adminapp.adminauth.persistence

import com.fasterxml.jackson.databind.ObjectMapper
import jakarta.persistence.*
import org.springframework.data.repository.CrudRepository
import org.springframework.stereotype.Repository
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RestController

@Entity
@Table(name = "a_test_entity")
class AdminTestEntity {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    var id: Int? = null

    var name: String? = null

    @Column
    @Convert(converter = StrListConverter::class)
    var interactions: List<Interaction> = emptyList()
}

enum class Interaction { HI, BYE }

class StrListConverter : AttributeConverter<List<Interaction>, String?> {
    override fun convertToDatabaseColumn(p0: List<Interaction>): String? = ObjectMapper().writeValueAsString(p0)
    override fun convertToEntityAttribute(p0: String?): List<Interaction> =
        p0?.let { listOf(*ObjectMapper().readValue(it, Array<Interaction>::class.java)) } ?: emptyList()
}

@Repository
interface AdminTestEntityRepository : CrudRepository<AdminTestEntity, Int?>

@RestController
@RequestMapping("/api/v1/admin/test")
class AdminTestController(private val adminTestEntityRepository: AdminTestEntityRepository) {
    @GetMapping("all")
    fun getAllTest() = adminTestEntityRepository.findAll()
}

The kicker for this whole issue is that it only seems to be List<Enum> that causes this issue.整个问题的关键在于似乎只是List<Enum>导致了这个问题。 For example, the following three re-definitions do not cause an instance of this issue:例如,以下三个重新定义不会导致此问题的实例:

var interactions: ArrayList<Interaction> = emptyList()
var interactions: List<String> = emptyList()
var interactions: List<DataClass> = emptyList()

What could be the cause of this?这可能是什么原因造成的? Why is it only List<Enum> ?为什么只有List<Enum>

It seems that https://hibernate.atlassian.net/browse/HHH-15624 fixed this issue.似乎https://hibernate.atlassian.net/browse/HHH-15624解决了这个问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM