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
|