-->
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.  [ 10 posts ] 
Author Message
 Post subject: Architecture question?
PostPosted: Thu Mar 16, 2006 4:40 pm 
Beginner
Beginner

Joined: Mon Feb 13, 2006 8:34 pm
Posts: 27
Hibernate version:3.0

I have a question about a composite key. If I use this kind of key (what is of course not recommended) I have approprieate properties inside my persistent object that store my composite key. Does additionally exist some associated object that store my coposite key?. Do I have to define class for object that store my key? I ask because this is how it works in JDO, that every composite key has related object.

And the second question is, if I don't define some id property in my persistent class, so id is store internally by Hibernate, how does Hibernate keep that id value. Is some object created for that id value.

thx...


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 16, 2006 10:13 pm 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
In hibernate you have the option to declare composite-ids as mapped or unmapped. If you map them (using class="xxx") then getId() will return an instance of the class you specify. If you don't map them, then the properties are available directly from the main class. Have a look at section 5.1.5 of the ref docs for more info.

If you don't define an <id> element in your class mapping, then the mapping won't compile, so your other question is moot.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 16, 2006 11:58 pm 
Newbie

Joined: Wed Mar 15, 2006 2:37 pm
Posts: 6
I'm interested in why you say composite keys are not recommended - after all it is perfectly valid to design your database with them.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Mar 17, 2006 1:52 am 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
From the ref docs, section 5.1.4, "id":
refdocs wrote:
There is an alternative <composite-id> declaration to allow access to legacy data with composite keys. We strongly discourage its use for anything else.

Some of us don't discourage their use, some do...


Top
 Profile  
 
 Post subject:
PostPosted: Fri Mar 17, 2006 3:10 am 
CGLIB Developer
CGLIB Developer

Joined: Thu Aug 28, 2003 1:44 pm
Posts: 1217
Location: Vilnius, Lithuania
"Good" identifier must be unique, not null and immutable, generate identifier if natural identity is "Bad".


Top
 Profile  
 
 Post subject: identity
PostPosted: Fri Mar 17, 2006 7:22 am 
Beginner
Beginner

Joined: Mon Feb 13, 2006 8:34 pm
Posts: 27
tenwit wrote:
In hibernate you have the option to declare composite-ids as mapped or unmapped. If you map them (using class="xxx") then getId() will return an instance of the class you specify. If you don't map them, then the properties are available directly from the main class. Have a look at section 5.1.5 of the ref docs for more info.

If you don't define an <id> element in your class mapping, then the mapping won't compile, so your other question is moot.


I know that defining <id> element im my class mapping file is necessary. But
I was not asking about id element im my mapping file but I was asking about how and where id is store in this case:
Code:
<id column="CATEGORY_ID">
<generator class="native"/>
</id>


Top
 Profile  
 
 Post subject:
PostPosted: Fri Mar 17, 2006 7:45 am 
Beginner
Beginner

Joined: Mon Dec 05, 2005 4:15 am
Posts: 36
kiml wrote:
I'm interested in why you say composite keys are not recommended - after all it is perfectly valid to design your database with them.



There are many problems if you are using the composite keys. They increase the complexity of your data access code.

E.g., if you update one part of the key, you must delete the old entity, and then persist the new entity - else will Hibernate insert the new object and keep the old, because they have defferent ID and from Hibernate sight they are 2 different objects.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Mar 17, 2006 3:13 pm 
Newbie

Joined: Wed Mar 15, 2006 2:37 pm
Posts: 6
If I have a parent child relationship I don't want to have to artificially create a primary key for the child records if their uniqueness can be determined by a composite key. This seems like bad design to me.


Top
 Profile  
 
 Post subject:
PostPosted: Sat Mar 18, 2006 12:46 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 7:19 pm
Posts: 2364
Location: Brisbane, Australia
Composite keys (unless used as a link table) seem to be a bad design smell to me. There are good reasons not use composite keys if you can avoid them. I cannot see an advantage of using them for a parent-child relationship.

The positive side is that its great that hibernate allows us to use what ever we wish.


Top
 Profile  
 
 Post subject:
PostPosted: Sun Mar 19, 2006 9:46 pm 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
Earlier you asked:
olobolo wrote:
I was not asking about id element im my mapping file but I was asking about how and where id is store in this case:

In the CATEGORY_ID column... I guess that I'm missing the point of your question, because you can't have wanted to know that. Can you rephrase the question?


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