Mysql 包含枚举集的结果集生成的表不';不存在

Mysql 包含枚举集的结果集生成的表不';不存在,mysql,spring,hibernate,kotlin,jdbc,Mysql,Spring,Hibernate,Kotlin,Jdbc,我有一个类似的枚举 enum class AmenitiesEnum { POOL, PETS_ALLOWED // shortened list, for demo purpose only } 这个枚举包含在一个类中,然后在MySQL数据库中持久化 @Entity @Table(name = "hotels") class Hotel ( @Id @GeneratedValue(strategy= GenerationType.AUTO) override val i

我有一个类似的枚举

enum class AmenitiesEnum {
POOL,
PETS_ALLOWED
// shortened list, for demo purpose only
}
这个枚举包含在一个类中,然后在MySQL数据库中持久化

@Entity
@Table(name = "hotels")
class Hotel (

@Id
@GeneratedValue(strategy= GenerationType.AUTO)
override val id: Int? = null,

override val name: String,

@Enumerated(EnumType.STRING)
val rating: RatingsEnum?,

@OneToMany(mappedBy = "hotel", cascade = [CascadeType.ALL])
val rooms : List<Room> = ArrayList<Room>(),

// below code seems to be the issue

@ElementCollection
@Enumerated(EnumType.STRING)
val amenities: Set<AmenitiesEnum> = EnumSet.noneOf(AmenitiesEnum::class.java)
) : Accommodation, Serializable
当我启动应用程序并对我得到的数据库进行查询时

... binding parameter [1] as [INTEGER] - [1]
... SQL Error: 1146, SQLState: 42S02
... Table '_database_.hotel_amenities' doesn't exist
... Resolved [org.springframework.http.converter.HttpMessageNotWritableException: Could not write JSON: could not extract ResultSet; nested exception is com.fasterxml.jackson.databind.JsonMappingException: could not extract ResultSet (through reference chain: java.util.ArrayList[0]->com.katanox.hotelbooking.domain.room.value.Room["hotel"]->com._.hotelbooking.domain.hotel.value.Hotel["amenities"])]
为什么MySQL状态
表“d035b60f.hotel_communications”不存在
,我如何解决这个问题?将类型更改为List而不是EnumSet会导致相同的错误

更新

查看调试,Hibernate将其转换为:

select
    amenities0_.hotel_id as hotel_id1_0_0_,
    amenities0_.amenities as amenitie2_0_0_ 
from
    hotel_amenities amenities0_ 
where
    amenities0_.hotel_id=?

我仍在寻找问题的解决方案,任何提示都将不胜感激。

为此,您需要一个自定义JPA属性转换器,将其映射到字符串。在JDBC级别,据我所知,这必须表示为逗号分隔的字符串。有关如何使用它的示例,请参阅文档:

,因为它是一个值的集合,不能在单个列中表示(至少从JPA的角度来看,如果不做额外的工作)。这也适用于
@ElementCollection
它需要知道如何映射,默认情况下,它使用外键将其映射到不同的表。
select
    amenities0_.hotel_id as hotel_id1_0_0_,
    amenities0_.amenities as amenitie2_0_0_ 
from
    hotel_amenities amenities0_ 
where
    amenities0_.hotel_id=?