Bear with me, since I'm a JPA newbie :-)
I have a simple one to many relationship where the parent has an ID and the children have the same as foreign key.
Now I'd like to replace all children, which (probably) means I have to delete all the existing children first and then merge the parent. Note that I can't just delete the parent and re-insert it, since it has relations to other tables.
So... all I need to do first is to delete all children. I load the parent and then
for ( Iterator<Child> iterator = parent.getChildren().iterator(); iterator.hasNext(); )
{
Child child = iterator.next();
child.setParent( null );
iterator.remove();
}
entityManager.merge( parent );
In that case I get
javax.ejb.EJBException: javax.persistence.PersistenceException: org.hibernate.PropertyValueException: not-null property references a null or transient value: ...Child.parent
If I don't set the parent to null, then the children are
not deleted.
I tried using a query to delete the children, however I don't have the foreign key column mapped directly, since I have the parent mapped in the child as
@ManyToOne( optional = true )
@JoinColumn( name = "parent_id", nullable = false )
private Parent parent;
This seems such a trivial problem, but searching around the Internet and looking it up in books didn't show me any usable solution...
Thanks for your help!
Eric
Btw, I'm using Hibernate in JBoss 5