【发布时间】:2015-03-04 11:23:30
【问题描述】:
我有一个 spring mvc 应用程序,它使用 hibernate 来处理 mysql 数据库。我有acl 和acl_group 表。这两个表与连接表有Many to Many 关系。下面是实体类的结构:
Acl:
public class Acl implements Serializable{
...
@ManyToMany(mappedBy = "aclCollection",fetch = FetchType.LAZY)
private Collection<AclGroup> aclGroupCollection;
//Setter and getter
}
AclGroup:
public class AclGroup implements Serializable{
...
@JoinTable(name = "acl_group_acl", joinColumns = {
@JoinColumn(name = "acl_group_id", referencedColumnName = "id")}, inverseJoinColumns = {
@JoinColumn(name = "acl_id", referencedColumnName = "id")})
@ManyToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
private Collection<Acl> aclCollection;
//Setter and getter
}
现在,我需要删除一个AclGroup 对象,并且我想删除acl_group_acl 表中的所有相关记录。但是当我尝试通过休眠删除AclGroup 对象时,什么也没有发生,我收到org.hibernate.exception.ConstraintViolationException 异常和Cannot delete or update a parent row: a foreign key constraint fails 消息。谁能帮我解决这个问题?
更新:
正如@JB Nizet 所说,我删除了cascade = CascadeType.ALL。现在,acl_group 记录和相关的“acl_group_acl”记录被删除。但是我遇到了以下异常:
org.springframework.http.converter.HttpMessageNotWritableException: Could not write content: No serializer found for class org.hibernate.proxy.pojo.javassist.JavassistLazyInitializer and no properties discovered to create BeanSerializer
【问题讨论】:
标签: java hibernate orm many-to-many