File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Object Relational Mapping and the fly likes cascade vs inverse Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "cascade vs inverse" Watch "cascade vs inverse" New topic
Author

cascade vs inverse

Mike Okri
Ranch Hand

Joined: Jun 22, 2011
Posts: 83
Am I correct in assuming that a cascade can cancel the effect of inverse. For example, if I specify inverse="true" in the parent mapping file (on a child collection), this implies that the child is responsible to maintaining the relationship and any update operation on the parent should not result in an update on the foreign key in the child table. However, if I specify cascade="save-update" in the parent mapping file (on a same child collection), this cancels out the inverse setting and cascades an update operation on the parent to the child table which results in an update on the foreign key in the child table.
Rishi Shehrawat
Ranch Hand

Joined: Aug 11, 2010
Posts: 218

I think it should not. You can write a small test case to check.
Ogeh Ikem
Ranch Hand

Joined: May 13, 2002
Posts: 180
Cascade and inverse are different concepts.

Cascade allows you to implicitly write Java code that produces data consistency, for example, if a parent has a child, the child should be saved in the database when the parent is saved. It's easy to save the parent and forget to save the child. Cascade allows you to ensure that when the parent is saved, the child is saved as well.

With cascade="save-update", the following code



is equivalent to



Inverse allows you to explicitly write Java code that produces SQL optimization. Because relationship changes can be made from both sides of a bidirectional relationship, you want to ignore relationship changes made from one side of the bidirectional relationship so that SQL is optimized, otherwise, you’ll get redundant updates. For example, in a parent-child bidirectional relationship, all the programmer has to do in order to produce optimized SQL is:
1) make the Parent side the inverse side so that the Child side is responsible for managing the relationship
2) ensure that when the Parent's children property is changed, a corresponding change is made to the Child's parent property because relationship changes that are made on the Child side are the changes that really matter. As long both are done together, the Java object graph will be correctly saved on disk.

For example, the following code correctly saves the object graph when adding a new Child to a Parent:



The following lines will not update the parent foreign key on the child table.



The following line will update the parent foreign key on the child table.




 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: cascade vs inverse
 
Similar Threads
Associations in Hibernate
Would like advise mapping Hibernate objects
auto insert/update/delete
Cascading non foreign key to Child Table
Hibernate One-to-Many foreign key