-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 
Author Message
 Post subject: Newbie.. Confused with hibernate cascade and inverse.
PostPosted: Wed Oct 08, 2008 2:56 am 
Newbie

Joined: Wed Oct 08, 2008 1:54 am
Posts: 2
Hi All,

I always wondered what cascade and inverse meant. I will tell what I have understood. Can someone please tell me if I am right or wrong?

Assumption:
Say there are two tables, parent and child. Parent has cid foreign key to child table's primary key childid.

Cascade: None--> If Parent table is inserted with values, then child table is not created if it does not exist and is not inserted with any value. cid is not propagated to childid

Cascade: All--> If Parent table is inserted with values, then even tho child is not already existing, it is created and child table's childid is inserted with cid of the Parent table

Inverse:true--> Both tables take responsibilities? But for what?
Inverse:false--> Only one table takes responsibility , Which one? and for what?

Thanks.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Oct 08, 2008 10:59 am 
Expert
Expert

Joined: Mon Nov 26, 2007 2:29 pm
Posts: 443
"cascade" on a collection of children, means that, whatever operation you perform in the parent, it will pass on to the children.

This is unrelated to DDL. Table creation, etc, is managed by other settings.

Id's are also unrelated to this command, they can be assigned or auto-generated, according to the strategy you specify in the mapping file.

cascade=none means that no parent operation is repeated on the children.
cascade=all means that all operations (save, persist, update, delete, merge) are repeated on the children.

The "inverse" property is used in bi-directional associations. This is, relations for which you have both parent.getChildren() and child.getParent().
When you are adding a new child, Hibernate expects you to take care, from the Java point of view, of both aspects of this association.
In other words, you have to do not only this
Code:
1.   parent.children.add(child)


but also this
Code:
2.   child.setParent(parent)


Hibernate has to detect that a child has been added, and generate the proper SQL. The problem is when to generate that SQL. In a unidirectional relationship, the only code you need is 1, so the SQL is generated there.
In a bidirectional relationship, you have both code 1. and 2., and Hibernate has to choose at which moment it generates the SQL.
If we set 1. (the one-to-many side) as "inverse", Hibernate will only generate SQL code when 2. occurs.

_________________
Gonzalo Díaz


Top
 Profile  
 
 Post subject:
PostPosted: Wed Oct 08, 2008 11:20 am 
Newbie

Joined: Wed Oct 08, 2008 1:54 am
Posts: 2
Hi,

Thanks for the reply. Can you give me another example maybe for inverse? I did not get it. I understood cascade tho.

Thanks.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.