-->
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.  [ 5 posts ] 
Author Message
 Post subject: Why does many-to-one attr causes an additional SQL Update?
PostPosted: Mon Oct 18, 2004 8:54 pm 
Beginner
Beginner

Joined: Fri Oct 15, 2004 9:58 pm
Posts: 25
I am very new to Hibernate and trying to understand how it works. I am not sure of the semantics of many-to-one association. Any help would be appreciated.

I have used the exmaple Cat class in the reference doc and written a sampe app.

O-R Mapping is below

Code:
      <class name="Cat" table="CATS" discriminator-value="C">
                <id name="id" column="uid" type="long">
                        <generator class="hilo"/>
                </id>
                <discriminator column="subclass" type="character"/>
                <property name="name"/>
                <property name="birthdate" type="date"/>
                <property name="color" not-null="true"/>
                <property name="sex" not-null="true"/>
                <property name="weight"/>
                <many-to-one name="mate" column="mate_id" cascade="all"/>
                <set name="kittens" cascade="all">
                        <key column="mother_id"/>
                        <one-to-many class="Cat"/>
                </set>
        </class>


The class definition is straight forward.

Code:
Cat a = new Cat(...);
Cat b = new Cat(...);

a.setMate(b);
b.setMate(a);

session.save(a);
session.save(b)


The above code generates the following SQL Statements

Hibernate: insert into CATS (name, birthdate, color, sex, weight, mate_id, subclass, uid) values (?, ?, ?, ?, ?, ?, 'C', ?)
Hibernate: insert into CATS (name, birthdate, color, sex, weight, mate_id, subclass, uid) values (?, ?, ?, ?, ?, ?, 'C', ?)
Hibernate: update CATS set name=?, birthdate=?, color=?, sex=?, weight=?, mate_id=? where uid=?


I understand that the update attribute of properties is not set false and so the update is generated. But even if I set update to true why should it generate this SQL statement.

When should I set update to true or false?


Top
 Profile  
 
 Post subject:
PostPosted: Tue Oct 19, 2004 1:30 am 
Pro
Pro

Joined: Tue Aug 26, 2003 8:07 pm
Posts: 229
Location: Brisbane, Australia
I'd say your problem has more to do with the one-to-many mapping than the one-to-many.
Have you read the relevant part of the reference doco that discusses mapping collections? Specifically, the part about the "inverse" attribute? This is probably what is going on.

This might help: http://www.hibernate.org/155.html.

If you need more info, there's lots more doco floating around about this on the forums, the wiki and the refernce doco.

_________________
Cheers,
Shorn.


Top
 Profile  
 
 Post subject: invers end
PostPosted: Tue Oct 19, 2004 2:32 am 
Newbie

Joined: Tue Oct 14, 2003 2:32 pm
Posts: 11
You can tell Hibernate to handle bidirectional associations with the attribute invers int the many side of the association, this way Hibernate will issue just the necesary sql statements
change your collection mapping to this and see what happens

<set name="kittens" cascade="all" inverse="true">
<key column="mother_id"/>
<one-to-many class="Cat"/>
</set>


Top
 Profile  
 
 Post subject: Re: invers end
PostPosted: Tue Oct 19, 2004 1:45 pm 
Beginner
Beginner

Joined: Fri Oct 15, 2004 9:58 pm
Posts: 25
I have removed the collection association altogether. There is no kittens proeprty any more. Even then I get the additional update. The additional update seems to be nothing to do with the one-to-many assoication but the many-to-one property mate_id.

If I do not set one of the cat's mate then I don't get this additional update.
That is if the code was like below (notice the commented out part) then there is no update SQL statement.

Code:
Cat a = new Cat(...);
Cat b = new Cat(...);

a.setMate(b);
//b.setMate(a);

session.save(a);
session.save(b)


I have read the relevant docs but I don't see any reference to this additional update that comes with many-to-one property.

I do not see why an additional update is required at all for many-to-one associations.

Prasad


rcancino wrote:
You can tell Hibernate to handle bidirectional associations with the attribute invers int the many side of the association, this way Hibernate will issue just the necesary sql statements
change your collection mapping to this and see what happens

<set name="kittens" cascade="all" inverse="true">
<key column="mother_id"/>
<one-to-many class="Cat"/>
</set>


Top
 Profile  
 
 Post subject: Because of the foreign key constraint.....
PostPosted: Tue Oct 19, 2004 5:05 pm 
Beginner
Beginner

Joined: Fri Oct 15, 2004 9:58 pm
Posts: 25
The extra update is needed because the many-to-one property creates a foreign key constraint and so an extra update is needed. As long as there is foreign key constrant this is a must.


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