在 @OneToMany 上级联删除时出现 Hibernate ConstraintViolationException
这似乎是一个非常简单的问题,但我已经为此苦苦挣扎了一段时间。我有两个实体 Client 和 User,其中 Client 是 User 的父级。实体注释如下:
Client:
@OneToMany(mappedBy = "client", fetch = FetchType.LAZY)
@Cascade({CascadeType.SAVE_UPDATE, CascadeType.DELETE})
private List<User> users = new ArrayList<User>();
User:
@ManyToOne(optional = false, fetch = FetchType.EAGER, cascade = {CascadeType.REFRESH})
@JoinColumn(name = "client_id")
private Client client;
public User(Client client, String userName, String password) {
client.getUsers().add(this);
}
我需要在客户端删除才能级联到用户。如果在一个 Hibernate 会话中创建客户端和用户并在另一 Hibernate 会话中删除客户端,则此方法有效。但是,如果我尝试在同一会话中删除客户端,则永远不会发出删除用户的命令,并且我会收到 org.hibernate.exception.ConstraintViolationException。
有人知道如何解决这个问题吗?
This may seem like a very simple question, but I have been struggling with it for a while. I have two entities Client and User where Client is a parent of User. The entities are annotated as follows:
Client:
@OneToMany(mappedBy = "client", fetch = FetchType.LAZY)
@Cascade({CascadeType.SAVE_UPDATE, CascadeType.DELETE})
private List<User> users = new ArrayList<User>();
User:
@ManyToOne(optional = false, fetch = FetchType.EAGER, cascade = {CascadeType.REFRESH})
@JoinColumn(name = "client_id")
private Client client;
public User(Client client, String userName, String password) {
client.getUsers().add(this);
}
I need deletion on the client to cascade to the user. This works if the client and user are created in one Hibernate session and the client is deleted in another Hibernate session. However, if I try to delete the client in the same session, then deleted on the user is never issued and I get org.hibernate.exception.ConstraintViolationException.
Does anybody know how to fix this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
事实证明,持久服务正在使用批量删除 HQL 语句删除客户端,该语句显然不会级联删除到子用户。
Well, it turns out that the persistent service was deleting the client using a bulk-delete HQL statement, which apparently does not cascade delete to child users.