-->
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.  [ 7 posts ] 
Author Message
 Post subject: many-to-one unique, cascade setting?
PostPosted: Sat Jul 30, 2005 12:17 pm 
Beginner
Beginner

Joined: Thu Jul 28, 2005 6:40 pm
Posts: 29
Hi;

HIA 6.3.1 say that for a 1:1 relationship (like Person:Address), the config is:
<class name="Person">
<id name="id" column="personId">
<generator class="native"/>
</id>
<many-to-one name="address"
column="addressId"
cascade="all"
unique="true"/>
</class>

<class name="Address">
<id name="id" column="addressId">
<generator class="native"/>
</id>
</class>

While the docs at http://www.hibernate.org/hib_docs/v3/re ... tional-121 (8.2.2) say it's:
<class name="Person">
<id name="id" column="personId">
<generator class="native"/>
</id>
<many-to-one name="address"
column="addressId"
unique="true"
not-null="true"/>
</class>

<class name="Address">
<id name="id" column="addressId">
<generator class="native"/>
</id>
</class>

So the question is... What are the correct settings for cascade and not-null when I have a table like Person where each row in person has exactly one matching row in Address?

And of the different approaches for 1:1 unidirectional mappings, where each Address row is owned by exactly 1 Person row - is this the best method? (HIA insinuates that this is the best approach - but never flat-out says it is.)

thanks - dave


Top
 Profile  
 
 Post subject:
PostPosted: Sat Jul 30, 2005 1:39 pm 
Newbie

Joined: Fri Jan 07, 2005 5:46 pm
Posts: 19
not-null="true" enforces that you need an address - it's good practice for a 1:1.

with cascade="all" you indicate that the address record will be handled transitively through Hibernate. see http://www.hibernate.org/hib_docs/v3/re ... transitive for details.

you could also model address as a component. there is no one answer to what is best.

Andreas


Top
 Profile  
 
 Post subject:
PostPosted: Sat Jul 30, 2005 2:22 pm 
Beginner
Beginner

Joined: Thu Jul 28, 2005 6:40 pm
Posts: 29
Hello;

I understand what both cascade and not-null do. My question is based on the fact that HIA uses just one and the online docs use just the other. So I am wondering why? My guess is that both usually make sense in a case like this - but then would both samples use both?

So my question is why was only one used in each case?

As to the which approach is best, my concern is I can guess - but being new to hibernate, if I make a guess each time I hit something like this, almost certainly some of the guesses will be wrong. So I was looking for advice on which approach is most efficient.

??? - thanks - dave

ps - I drill in on questions like this because I was a senior developer on the Windows team at Microsoft and I saw a lot of very smart people who did not know the Windows code make very reasonable decisions - that were not optimal if you knew the internals of Windows. So I try to get the why on things from people who know the underlying code very very well.


Top
 Profile  
 
 Post subject:
PostPosted: Sat Jul 30, 2005 2:29 pm 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
There is no difference in the basic mapping, you are simply looking at two different examples.


Top
 Profile  
 
 Post subject:
PostPosted: Sat Jul 30, 2005 2:36 pm 
Beginner
Beginner

Joined: Thu Jul 28, 2005 6:40 pm
Posts: 29
Hi;

Ok, then I am not understanding something. My understanding of HIA is that cascade='all' is necessary so that changes to the Person row propigate to the address row in the DB.

Are you saying that updates/deletes will propigate without cascade='all'?

??? - thanks - dave


Top
 Profile  
 
 Post subject:
PostPosted: Sat Jul 30, 2005 2:46 pm 
Newbie

Joined: Fri Jan 07, 2005 5:46 pm
Posts: 19
no they won't. for example, if you delete a person object, the associated address will still exist, unless you use cascade="delete". same for other operations.


Top
 Profile  
 
 Post subject:
PostPosted: Sat Jul 30, 2005 2:49 pm 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
Sure, but you can also call session.save(address) or whatever. Cascading is always optional. That's exactly what is written in HiA: "Probably, cascade=all makes sense..." because you almost always want transitive persistence and/or lifecycle for one-to-one relationships.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 7 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:
cron
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.