嗨,我正在使用 JPA2 和 Hibernate 实现,我得到了一个像这样的简单映射:
@Entity
@Table(name = "users")
public class User {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
@OneToMany(mappedBy = "user", cascade = ALL)
private List<SubscribedUser> subscribedUsers;
}
和第二个表 (SubscribedUser) 与 id 类:
@Entity
@Table(name = "subscribed_users")
@IdClass(SubscribedUserId.class)
public class SubscribedUser {
@Id
@ManyToOne
@JoinColumn(name = "id_user", referencedColumnName = "id")
private User user;
@Id
@ManyToOne
@JoinColumn(name = "id_subscribed_user", referencedColumnName = "id")
private User subscribedUser;
}
假设我们有 2 条记录是 subscribed_users 表:1。
user | subscribed_user
1 | 2
2.
user | subscribed_user
2 | 1
问题是,当我删除 id = 1 的用户时。使用 cascade 选项正确删除了第一条记录,但出现错误,因为在 subscribed_users 表的第二个条目中仍然有对 id = 1 的用户的引用. 是否有可能级联删除第二条记录?
谢谢
大卫