[jboss-user] [EJB 3.0] - Re: OneToMany Unidirectional EntityManager remove() problem

jaikiran do-not-reply at jboss.com
Tue May 20 02:39:15 EDT 2008


"rkiran81" wrote : Hi JaiKiran,
  | 
  | Still am getting the same error.
  | 
Please post the latest exception stacktrace and the code changes that you have done. While posting the logs or xml content or code, remember to wrap it in a code block using the Code button in the message editor window and please hit the Preview button to make sure your post is correctly formatted. Also, are you sure that your changes were picked up? 

"rkiran81" wrote : 
  | CascadeType.ALL takes care of deleting refrenced child entries also know, then how does org.hibernate.annotations.CascadeType.DELETE_ORPHAN help. 

Well, that's what i thought earlier. I expected the child to be deleted from the DB when you remove it from a collection and save the parent. But thinking more about it and reading through this thread http://forum.hibernate.org/viewtopic.php?p=2376122, i understand that the org.hibernate.annotations.CascadeType.DELETE_ORPHAN is required in this case. In the scenario, we are discussing, we remove the child from a collection of the parent and persist the parent. While persisting the parent Hibernate sees that the "association" between the parent and the child has been removed and hence tries to set the parent id in the child table to NULL. If that column allows NULL then you won't see any exception but if it doesn't (which means that the child cannot exist without a parent) then it throws the constraint violation. So if you want Hibernate to remove the child when the "association" with its parent is removed then you have to add the org.hibernate.annotations.CascadeType.DELETE_ORPHAN annotation (Thinking more about this common scenario, i don't know why JPA does not provide an annotation for this instead of relying on vendor specific annotation). 



View the original post : http://www.jboss.com/index.html?module=bb&op=viewtopic&p=4151911#4151911

Reply to the post : http://www.jboss.com/index.html?module=bb&op=posting&mode=reply&p=4151911




More information about the jboss-user mailing list