【发布时间】:2013-01-01 01:21:00
【问题描述】:
我使用 Hibernate 3.6,我有这样的东西:
@Entity
public class Parent {
@OnyToMany( fetch = FetchType.LAZY, cascade = { ascadeType.ALL } )
@Cascade( { org.hibernate.annotations.CascadeType.SAVE_UPDATE, org.hibernate.annotations.CascadeType.DELETE )
@JoinColumn( name="Parent_ID" )
public List<Child> getChildren() { return children; }
public void setChildren( List<Child> children ) { this.children = children; }
private transient List<TitleMetadataCategory> children;
...
}
@Entity
public class Child {
....
}
关联是单向的,有几个原因,我不想改变它。此外,孤儿不存在,因此存在 CHILD.PARENT_ID 不为空的 DB 约束。 一切正常,除了删除孩子。当我这样做时
parent.getChildren().remove(child);
session.saveOrUpdate(parent)。
它失败了。
因为我没有
@ManyToOne( optional=false )
在子节点,Hibernate 尝试使用 PARENT_ID=NULL 更新子节点,但由于 DB 约束而失败。
有什么办法可以解决吗?
【问题讨论】:
-
单向休眠不允许将关联映射到没有连接的实体。
-
它已通过映射的 DB 中的 Child.Parent_ID 列连接
标签: java hibernate jpa-2.0 hibernate-mapping