-->
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.  [ 1 post ] 
Author Message
 Post subject: composite keys and many-to-one mappings
PostPosted: Wed Jul 08, 2009 7:41 pm 
Newbie

Joined: Wed Jul 08, 2009 1:29 pm
Posts: 1
Hi Everyone,

Let me start of by saying that I am new to Hibernate and ORM concepts so I apologize for any misuse of terms and unfamiliarity of concepts below.

For the sake of simplicity, I have two Classes; State and Country which are setup as follows:
NOTE: fields preceded by '#' are either a unique key or part of a composite unique key.
Country
----------
#String mName
float mSomeData

State
-----------
#String mName
#Country mCountry
float mSomeData

Given the above, you can not have two Country objects with the same name. You can have multiple State objects with the same name, provided that each one is associated with a different Country.

Now what I would like to do is be able to (in this order):
-Create a new Country called myCountry and assign it the name "myCountry".
-Create a new State called myState, assign it the name "myState" and set mCountry = myCountry.
-Doing a lookup on State would then -automatically- (I already know how to do this manually) lookup mCountry and then use the result of that lookup to do the lookup of myState.

The documentation I've read encourages the use of a single field dedicated to being the unique primary key, typically of type Long. I've implemented something that uses this along with many-to-one mappings and it works just fine, if you know your object's id. If you dont, the code gets bloated with recursive calls to load objects using Criteria...which personally seems no better than just writing SQL statements and not necessary if I can tell Hibernate that all my objects should be unique based on a composite key. I know that Hibernate supports these composite keys (and frowns upon them) but the load/get methods still require the unique key to be supplied in the lookup. How exactly does that work with composite keys? Or does it?

Am I going down the wrong path here?

Thanks!
Nick


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

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.