If a parent object starts off with a list of 3 children objects, removes child 1 and calls session.update(parent), what kind of hibernate mapping would I need in order to clear the parent PK from the removed child table without deleting the removed child completely?
I also have a constraint which prevents any parent from being deleted if any child is assign to it and another constraint that prevents me from accidentally cascade-deleting any child entries.
I can successfully cascade update children by simply calling session.update(parent), but I’m having trouble doing the reverse.
public class Parent implements Serializable {
private static final long serialVersionUID = 1L;
private Long id;
private List<Child> children;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
@OneToMany(mappedBy = "parent")
@org.hibernate.annotations.Cascade(
value = {org.hibernate.annotations.CascadeType.SAVE_UPDATE})
public List<Child> getChildren() {
return children;
}
public void setChildren(List<Child> children){
this.children = children;
for(Child child : children) {
child.setParent(this);
}
}
//----------------- Would I need something like this? ---------------
//----------------- Or does hibernate have a better way? ---------------
public void setChildren(List<Child> children){
for(Child child : this.children)
if (!children.contains(child)) {
child.setParent(null);
}
}
this.children = children;
for(Child child : children) {
child.setParent(this);
}
}
//----------------------------------------------------------------
}
public class Child implements Serializable {
private static final long serialVersionUID = 1L;
private Long id;
private Parent parent;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
@ManyToOne
public Parent getParent() {
return parent;
}
public void setParent(Parent parent) {
this.parent = parent;
}
}
This solution is similar, but I’m not deleting the Parent, instead I’m removing a Child from the List:
If you don’t have a not-null constraint on the FK parent field in the child table, you can just set parent to null and save the child.
I’d write the following method:
I’d be careful about overwriting the setChildren method, as hibernate needs plain getters and setters.