Cascade.Remove不能为OneToOne工作

时间:2015-10-21 18:58:05

标签: java hibernate jpa spring-data spring-data-jpa

我有像

这样的单向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;
}

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

org.postgresql.util.PSQLException:错误:更新o删除<>违反表<>

中的外键fk_citizen_file_id

为什么会这样?

1 个答案:

答案 0 :(得分:2)

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

因此,在删除FileEntity之前手动删除Citizen,或者在数据库中使外键可以为空。

相关问题