我正在尝试在 Springs 中创建一个简单的 ManyToOne 关系
// City.kt
package com.example.helpme.entity
import javax.persistence.*
import javax.validation.constraints.*
@Entity
data class City(
@Id id: Int,
var name: String,
@ManyToOne(optional = false) state: State
)
// State.kt
package com.example.helpme.entity
import javax.persistence.*
import javax.validation.constraints.*
@Entity
data class State(
@Id id: Int,
var name: String,
@OneToMany cities: Set<City> = emptySet(),
)
但是 Intelij Idea 告诉我 'One To Many' attribute value type should not be '? extends City'
当我尝试运行时,我得到了
org.springframework.beans.factory.BeanCreationException:创建在类路径资源[org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaConfiguration.class]中定义的名为“entityManagerFactory”的bean时出错:调用init方法失败;嵌套异常是 org.hibernate.AnnotationException:Collection 既没有定义泛型类型,也没有定义 OneToMany.targetEntity():com.example.helpme.entity.State.products
最佳答案
将 Set
更改为 MutableSet
对我有用
// State.kt
package com.example.helpme.entity
import javax.persistence.*
import javax.validation.constraints.*
@Entity
data class State(
@Id id: Int,
var name: String,
@OneToMany cities: MutableSet<City> = mutableSetOf(),
)
关于spring - Kotlin Hibernate 中删除 ManyToOne 列表类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66480713/