Hibernate 4使用oneToMany集合删除实体

时间:2014-08-21 13:55:52

标签: java hibernate

我试图从DB中删除用户实体

//whatever
@OneToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL, mappedBy = "user", orphanRemoval = true)
    private Set<Filter> filters = new HashSet<Filter>();
//whatever

//whatever
@ManyToOne(fetch = FetchType.LAZY, cascade = {CascadeType.MERGE, CascadeType.PERSIST, CascadeType.REFRESH, CascadeType.DETACH})
@JoinColumn(name = "user_id", nullable = false)
private User user;
//whatever

但得到: 错误SqlExceptionHelper:146 - 无法删除或更新父行:外键约束失败(web_app_dbfilters,CONSTRAINT FK_8oeay5yddrcgd4i71m1yda1hb FOREIGN KEY(user_id)REFERENCES {{1 } {(users))

orphanRemoval = true不工作,我该怎么办?

1 个答案:

答案 0 :(得分:0)

试试这个:

@ManyToOne(fetch = FetchType.LAZY, cascade = {CascadeType.ALL})
@JoinColumn(name = "user_id", nullable = false)
private User user;