簡體   English   中英

Spring Boot - 帶有 Kotlin 列表的實體<enum>不開機</enum>

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

我一直在幫助我的團隊將我們的 Maven/SpringBoot/Kotlin 項目從 Spring-Boot 2.7.5 升級到 Spring-Boot 3.0.0。 然而,啟動時的一個問題阻礙了我們的進步。 在 Spring-Boot 3.0.0 之前,這不是問題。

啟動應用程序后,我收到以下堆棧跟蹤:

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')

在花了一天時間檢查配置並隔離問題后,我們的應用程序中只剩下一個實體,我們仍然遇到問題。 然后我們開始從實體中刪除字段,直到應用程序能夠運行。 我們刪除的字段是Interaction類型的kotlin.collections.List ,這是我們為應用程序定義的枚舉類。

為了確保隱私,這里是將復制此問題的應用程序 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()
}

整個問題的關鍵在於似乎只是List<Enum>導致了這個問題。 例如,以下三個重新定義不會導致此問題的實例:

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

這可能是什么原因造成的? 為什么只有List<Enum>

似乎https://hibernate.atlassian.net/browse/HHH-15624解決了這個問題。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM