Hibernate 级联:对象引用未保存的瞬态实例 - 在刷新之前保存瞬态实例

发布于 2024-12-29 10:44:48 字数 758 浏览 0 评论 0原文

@Entity
@Table(name = "parent")
public final class Parent extends Base {

@OneToOne(cascade = CascadeType.PERSIST, fetch = FetchType.EAGER)
private Person person;

并做(除其他外):

Parent parent = new Parent();
Person person = new Person();
parent.setPerson(person);
session.save(parent);

我得到了提到的例外?

我之前需要手动调用 session.save(person) 吗?我是否必须向子类定义(引用父类的位置)添加级联类型注释?

或者我错过了其他明显的事情?

我不想使用 CascadeType.ALL 因为当删除父级时我想保留该人(子级)。

两个实体/表都扩展了一个公共基表:

@MappedSuperclass()
public abstract class Base {

    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    private Integer id;

    public Integer getId() {
    return id;
    }

这会影响所需的级联类型吗?

@Entity
@Table(name = "parent")
public final class Parent extends Base {

@OneToOne(cascade = CascadeType.PERSIST, fetch = FetchType.EAGER)
private Person person;

and doing (amongst other things) this :

Parent parent = new Parent();
Person person = new Person();
parent.setPerson(person);
session.save(parent);

I get the mentioned exception ?

Do I manually need to call session.save(person) before ? do I have to add a cascade type annotation to the childs class definition(where it references the parent) ?

Or have I missed something else obvious ?

I don't want to use CascadeType.ALL as when a parent is deleted I want to keep the person(child).

Both entities/tables extend a common Base table :

@MappedSuperclass()
public abstract class Base {

    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    private Integer id;

    public Integer getId() {
    return id;
    }

Will this effect which cascade type is required ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

此刻的回忆 2025-01-05 10:44:48

您没有提到 Hibernate 版本,但自从我开始使用它以来,这一点没有改变。

正如您可以在 Hibernate 参考,要获得 Java 标准 SAVE_UPDATE,您需要在 Hibernate 中使用 {CascadeType.PERSIST, CascadeType.MERGE}

编辑:查看更新的信息,您现在所做的操作会导致 Hibernate 将其视为双向一对一映射。这基本上意味着,对于这两个表中任何一个中的每个对象,必须在另一个表中具有相同 ID 的对应对象。因此,您不能只删除其中之一,否则您将失去 FK 完整性。

如果您希望它是单向映射,例如,如果您希望能够删除该人但保留父级 - 您必须指定一个 FK,通常通过 @JoinColumn,例如
@JoinColumn(name="PERSON_ID", unique=false, nullable=true, insertable=true, updatetable=true)

You haven't mentioned the Hibernate version, but this hasn't changed since I ever started using it.

As you can read in the Hibernate reference, to get the Java standard SAVE_UPDATE you need {CascadeType.PERSIST, CascadeType.MERGE} in Hibernate.

EDIT: Seeing the updated info, what you're doing now causes Hibernate to treat it as a bi-directional one-to-one mapping. This basically means that for each object in any of those two tables, there has got to be a counterpart in the other table with the same ID. Therefore, you cannot delete only one of them, you would lose FK integrity.

If you want it to be a unidirectional mapping, e.g., if you want to be able to delete the person but leave the parent -- you have to specify a FK, usually via @JoinColumn, like
@JoinColumn(name="PERSON_ID", unique=false, nullable=true, insertable=true, updatable=true)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文