I have the following code that according to hibernate logging sql results in
1. insert parent
2. insert child
3. update parent(why is this happening?)
Here is the transaction code....
Code:
mgr.getTransaction().begin();
ParentDBO parent = new ParentDBO();
parent.setName("parent");
ChildDBO child = new ChildDBO();
child.setName("child");
parent.addChild(child);
mgr.merge(parent);
mgr.getTransaction().commit();
Here is the relevant ParentDBO code
Code:
@Column(name="parent_id")
@OneToMany(mappedBy="parent", cascade = CascadeType.ALL, fetch=FetchType.EAGER)
public Collection<ChildDBO> getChildren() {
if(children == null) {
children = new ArrayList<ChildDBO>();
}
return children;
}
public void setChildren(Collection<ChildDBO> children) {
this.children = children;
}
public void addChild(ChildDBO child) {
if(child == null)
throw new IllegalArgumentException("child may not be null and was");
else if(child.getParent() != null)
child.getParent().getChildren().remove(child);
child.setParent(this);
getChildren().add(child);
}
public void removeChild(ChildDBO child) {
if(child == null)
throw new IllegalArgumentException("child may not be null and was");
else if(!getChildren().contains(child))
throw new IllegalArgumentException("Parent does not contain this child");
child.setParent(null);
getChildren().remove(child);
}
Here is the relevant ChildDBO code....
Code:
@ManyToOne
@JoinColumn(name="parent_id")
public ParentDBO getParent() {
return parent;
}
public void setParent(ParentDBO parent) {
this.parent = parent;
}