gpt4 book ai didi

java - Toplink 尝试保留空对象

转载 作者:行者123 更新时间:2023-12-01 16:09:46 25 4
gpt4 key购买 nike

我有一个对象“实例”,里面有另一个对象“类(class)”。当尝试保留新的 Instance 对象时,如果 Course 为 null,我会收到以下错误:

java.lang.IllegalStateException: During synchronization a new object was found through a relationship that was not marked cascade PERSIST: null

这是我的类(class)注释:

@OneToOne(cascade = CascadeType.REFRESH,optional=true)
public Course getCourse() {
return course;
}

我不明白为什么它试图保留空的东西。如果它为空,那么应该在数据库中将该字段保留为空,对吧?

最佳答案

I don't see why it's trying to persist something that's null. If it's null, it should just leave that field null in the database right?

默认情况下,TopLink JPA 假定实体的所有字段都是持久的。如果你想指定一个非持久化实体的字段或属性,你需要用@Transient来标记它(但这不是你想要的)。

因此,对我来说,此错误是由于没有为 Instance 实体中关系的 OneToOne 端指定正确的级联类型而导致的。我无法说出为什么您不使用 ALL,但您至少需要添加 PERSIST

关于java - Toplink 尝试保留空对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1703429/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com