gpt4 book ai didi

java - Cascade.Remove 不适用于 OneToOne

转载 作者:行者123 更新时间:2023-12-02 04:16:23 24 4
gpt4 key购买 nike

我有一个单向 OneToOne 关系,例如

@Entity   
public class Citizen
{
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
protected Long id;

@OneToOne(cascade = CascadeType.ALL)
protected FileEntity photo;
...getters and setters....
}

@Entity
public class FileEntity
{
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
protected Long id;
}

然后,当我去删除公民对象时,出现以下错误:

org.postgresql.util.PSQLException:错误:<>上的更新或删除违反了表<>中的外键fk_citizen_file_id

为什么会发生这种情况?

最佳答案

Hibernate在the order they were performed中的数据库中执行删除操作.

因此,要么在删除 Citizen 之前手动删除 FileEntity,要么使数据库中的外键可为空。

关于java - Cascade.Remove 不适用于 OneToOne,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33267129/

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