我有一个带有复合主键的父实体:
public class Parent{
@EmbeddedId
private ParentPK id;
@OneToMany(fetch = FetchType.LAZY, mappedBy = "parent")
private List<Child> Children = new ArrayList<Child>();
...
}
我的复合主键类就像下面这样:
@Embeddable
public class ParentPK implements Serializable {
/** The id Parent. */
@Column(name="id_Parent", nullable=false)
private Integer idParent;
/** The n version Parent. */
@Column(name="n_version_Parent", nullable=false)
private Integer nVersionParent;
...
}
我的孩子实体:
public class Child {
...
/** The Parent. */
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumns({
@JoinColumn(name = "id_Parent", referencedColumnName = "id_Parent", nullable = false),
@JoinColumn(name = "n_version_Parent", referencedColumnName = "n_version_Parent", nullable = false)
})
private Parent parent;
...
}
我想做的是从数据库中删除父行,但不删除所有子行,不仅如此,我还想更新父行的版本号并将其强制为0。
这是我的删除DAO以删除父对象:
@Override
public void delete(final E entity) {
entityManager.remove(entityManager.merge(entity));
}
我的ChildDAO更新孩子:
public Boolean updateChildrenWhenParentIsDeleted(
final List<Child> children,
final Integer idParent) {
if (Children != null && Children.size() < 0) {
for (final Child child: children) {
final Parent parent= new Parent();
parent.setId(new ParentPK(idParent, 0));
child.setParent(parent);
}
}
getEntityManager().merge(children);
return true;
}
以及我的服务可以致电给我的ParentDAO和ChildrenDAO
final List<Child> res = ChildDAO.getChildren(parentPK.getIdParent(),
parentPK.getNVersionParent(), null);
parentDAO.delete(parent);
parentDAO.updateChildrenWhenParentisDeleted(res,
idParent);
有没有办法告诉休眠和数据库忽略我的两个表Parent和Chidren之间的约束?