我有一对多的关系:
@Entity
@Table(name = "Users")
public class User {
@Id
@Column(name = "user_id", nullable = false)
@GeneratedValue(strategy = GenerationType.AUTO)
private Integer id;
@Column(name = "login", nullable = false)
private String login;
@Column(name = "password", nullable = false)
private String password;
@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "role_id", nullable = false)
private Role role;
@OneToMany(fetch = FetchType.LAZY, mappedBy = "user", cascade = javax.persistence.CascadeType.ALL)
private Set<Contacts> contacts = new HashSet<Contacts>();
我正在尝试删除User
所有联系人的对象;我尝试使用:
cascade = javax.persistence.CascadeType.ALL
cascade = javax.persistence.CascadeType.REMOVE
@Cascade(CascadeType.DELETE)
从org.hibernate.annotations
@Cascade(CascadeType.DELETE_ORPHAN)
从org.hibernate.annotations
但没有任何帮助。我总是遇到异常:
org.hibernate.util.JDBCExceptionReporter - 无法删除或更新父行:外键约束失败 (
contactmanager
.contact
, CONSTRAINTcontact_ibfk_1
FOREIGN KEY (user_id
) REFERENCES
删除用户的UPD 代码如下:
@Transactional
public void removeUser(User user) {
sessionFactory.getCurrentSession().delete(user);
}
我将不胜感激任何帮助!谢谢。