我试图在spring中创建一个简单的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告诉我'一对多'属性值类型不应该是'?扩展的城市'
当我试着跑的时候我得到
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaConfiguration.class]: Invocation of init method failed; nested exception is org.hibernate.AnnotationException: Collection has neither generic type or OneToMany.targetEntity() defined: com.example.helpme.entity.State.products
ChangeSet
toMutableSet
works to me
// 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(),
)