spring - Kotlin Hibernate 中删除 ManyToOne 列表类型

标签 spring hibernate kotlin

我正在尝试在 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/

相关文章:

java - Hibernate 4 架构更新

hibernate - 在 entitymanager 上使用 unwrap 方法获取 native hibernate session 后,我是否必须同时关闭这两个 session ?

collections - Kotlins扩展方法Stream.toList()丢失

java - 如何检索@Async方法的结果

spring - 如何移动 Atomikos 的 tm.out 和 *.epoch 文件的位置?

java - 使用 hibernate 进行缓慢的嵌入式数据库处理

numbers - Kotlin:有没有办法清除数字重载?

kotlin - 不使用maven应用本地jar-plugin

java - 在查询注释中更新时间戳字段 Spring JPA

java - 按父字段查询但返回子类