我正在尝试使用以下实体的Hibernate注释实现软删除:
@MappedSuperclass
public abstract class AbstractEntity implements Serializable {
...
@Column(name = "DELETED")
protected boolean deleted = false;
...
}
@Entity
@Table(name = "TARGET")
@Where(clause = "deleted = '0'")
@SQLDelete(sql = "UPDATE target SET deleted = '1'")
public class Target extends AbstractEntity {
...
@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
@JoinColumn(name = "TARGET_ID")
private Set<Recording> recordings;
...
}
@Entity
@Table(name = "RECORDING")
@Where(clause = "deleted = '0'")
@SQLDelete(sql = "UPDATE recording SET deleted = '1'")
public class Recording extends AbstractEntity {
...
@ManyToOne
@JoinColumn(name = "TARGET_ID")
private Target target;
...
}
删除本身是对EntityManager的调用:
entityManager.remove(target);
此调用后,Target及其所有录制内容都已按预期删除标记设置为TRUE。 但是记录也将TARGET_ID列设置为NULL,这非常不幸,因为关系丢失了。这不是我所期望的,恢复状态是不可能的。 我希望TARGET_ID仍然能够引用Target。
我做错了吗?或者我是否必须手动为每个录音调用删除而不使用级联?
完成Spring Data JPA 1.11.4,Hibernate 5.0.12和JPA2.1。
感谢您提供任何提示
答案 0 :(得分:0)
你的@OneToMany从Target到Recordings的关系是不正确的。尝试:
@OneToMany(mappedBy="target", cascade = CascadeType.ALL, fetch = FetchType.LAZY)
private Set<Recording> recordings;