I use Hibernate 3.6 and I have something like this:
@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 {
....
}
Association is unidirectional for several reasons and I don't want to change it . In addition orphan children don't exist, so there is DB constraint that CHILD.PARENT_ID is not null. All works fine, except removing child. When I do
parent.getChildren().remove(child);
session.saveOrUpdate(parent)
.
it fails.
Since I don't have
@ManyToOne( optional=false )
at the child side Hibernate tries to update child with PARENT_ID=NULL and fails due to DB constraint.
Is there any way to fix it?