我正在尝试将Kotlin与JPA一起使用,并且基本上得到了两个应该一起工作的类。我在如何使数据类作物可序列化方面遇到问题。我尝试将@Serializable添加到Crops,但是IntelliJ告诉我Crops没有构造函数。这是两个类:
@Entity
data class Crops(
val amountOwned: Int = 0
) {
@Id
@ManyToOne(fetch = FetchType.EAGER)
lateinit var user: User
@ManyToOne(fetch = FetchType.EAGER)
lateinit var plant: Plant
constructor(user: User, plant: Plant) : this() {
this.plant = plant
this.user = user
}}
@Entity
data class User (
@Id
val username: String = "",
@get: NotBlank
val password: String = "",
@get: NotBlank
val salt: String = "",
@OneToMany(mappedBy = "user")
val crops: MutableSet<Crops> = HashSet()
已申请所有帮助!谢谢:)
答案 0 :(得分:0)
您必须为作物创建一个IdClass,因为JPA需要为诸如EntityManager.find()之类的方法创建一个类。
data class CropsId (val username: String = "")
然后将其与@IdClass一起使用
@IdClass(CropsPK::class)
@Entity
data class Crops(
val amountOwned: Int = 0
) {
@Id
@ManyToOne(fetch = FetchType.EAGER)
lateinit var user: User
@ManyToOne(fetch = FetchType.EAGER)
lateinit var plant: Plant
constructor(user: User, plant: Plant) : this() {
this.plant = plant
this.user = user
}}