-->
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.  [ 6 posts ] 
Author Message
 Post subject: "inverse" and "cascade" attributes......
PostPosted: Wed Oct 19, 2005 4:31 pm 
Beginner
Beginner

Joined: Mon Feb 23, 2004 5:11 pm
Posts: 39
Hibernate version:

H3.0.5


I am confused on the usage and purpose of the inverse and cascade attributes in the collection mapping.

In the following bidirectional one to many association:


Class A has a bag of Class B with inverse="true" and cascade = "all"
Class B has a many to one association with class A.

According to the documentaion, changes made only to the inverse end wont be persisted.

But I thought, by setting the cascade="all" , I could insert A and consequently insert all associated class Bs too.

What am I missing.....


Top
 Profile  
 
 Post subject:
PostPosted: Wed Oct 19, 2005 6:53 pm 
Beginner
Beginner

Joined: Tue Nov 25, 2003 3:33 pm
Posts: 35
you are not missing anything. Try it out with inverse="false" and with inverse="true" and turn show_sql flag.

You will see that with inverse=false set on class A's one-to-many element there is one more update. Hibernate will insert a record of B first and then update a_id on that record, while with inverse=true inserts will be performed with the a_id already in insert stmt.

Also, if you mark a_id of class B as non-nullable you will have JDBCExceptions thrown as hibernate will be inserting nulls to update them next.

HTH,
Alex.


Top
 Profile  
 
 Post subject: Not about the same behaviour
PostPosted: Wed Oct 19, 2005 7:04 pm 
Newbie

Joined: Wed Sep 17, 2003 3:31 am
Posts: 10
Location: Napoli (IT)
The inverse attribute tells Hibernate if the entity persister manages the collection.

If inverse=true the persister won't manage the collection. So it has to be managed by the other end.
This is not about cascading but establishing who has to write in the database the fact that B belongs to the collection defined in A.

So, in your example the relational representation of the collection is given by a FK A_id in table B.
If you make an instance of B part of the collection A.bagOfBs you have to set that column with the value of the A id.

When inverse is false, the persister of A do manage the collection, so the session will issue a command like: update B set A_id = ?.
Usually you don't want this in many-to-one associations, because the FK column is defined as a one-to-many property on the other side, so an update command for B already set that column. Hence in many-to-one associations you set inverse=true.

Cascading is not related to this behavior. It rather establishes how save, update, save-or-update or replicate operations applied to an entity instance, will apply to the associated instances.
For example, if the association between A and B has cascade=all, as you associate a new instance of B to the collection in A, it will be automatically saved. Or, if you delete A, Hibernate will also delete all Bs.

Hope this help... Indeed, I think the manual is much cleaner...

_________________
--- stefano
Don't forget to rate.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Oct 20, 2005 10:08 am 
Beginner
Beginner

Joined: Mon Feb 23, 2004 5:11 pm
Posts: 39
tnx guys. So, if I understand this correctly....

cascade specifies "if and what " associated entities would be persisted and
inverse specifies "how or who" would be doing the persisting.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Oct 20, 2005 10:09 am 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
The first thing you should realize about both is that they are not related. At all. Nothing. Nada. They are responsible for completely different things. Once you have made that step, read the documentation for both again.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Oct 20, 2005 10:14 am 
Beginner
Beginner

Joined: Mon Feb 23, 2004 5:11 pm
Posts: 39
ignore my previous post on who, what, if etc...
reading your posts a few more times made it clear. thanks again.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 6 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.