posted 12 years ago
OK I am not sure if you just did not read my last post or if you did not understand it.
As James said lets simplify this and keep it just about Cascade.REMOVE for moment.
Say you have this in your Division (Note that having both CascadeType.ALL and cascadeType.REMOVE is redundant)
Now like you example lets have this for Item
Now lets take a look at a simple example.
Ok so far so good. Now lets try to delete an Item. This is where I think you ignored my last post
Ok so to summarize. Running this with the Cascade.REMOVE on the @ManyToOne has the undesirable effect of deleting all the items and the division!! Taking the cascade.Remove off will result in an exception. Lets modify the code now so that it will work. It now looks like this. The cascade rules are the same on the @OneToMany
Ok now hopefully you understand Cascade.Remove.
Lets talk about orphan removal. Orphan removal should only be used on a very specific type of parent child relationship. It can only be added on @OneToMany and @OneToOne.
Orphan removal rules:
rule1 - child entity may only be a child of one parent entity, and may not ever belong to a different parent.
rule2- the child entity can not exist without its parent.
The the orphanRemoval element causes the child entity to be removed when the relationship between the parent and the child is broken. This can be done in 2 ways
1 setting to null the attribute that holds the related entity
2. in the one-to-many case you can remove the child entity from the collection.
The provider is then responsible for removing the orphaned child entity.