-->
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.  [ 4 posts ] 
Author Message
 Post subject: querying for composite key
PostPosted: Tue Aug 14, 2007 2:53 am 
Newbie

Joined: Tue Aug 14, 2007 2:46 am
Posts: 7
Hey guys,

I am trying to query for a class that is made up of a composite primary key using the session.get(Class, Serializable) method. Pseudocode:

ie.

test.hbm.xml
<composite-key>
<key fname>
<key lname>
</composite-key>


want to

session.get(test.class, ????);

what should go in the question marks???

thanks in advance.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 14, 2007 3:10 am 
Expert
Expert

Joined: Fri Jul 13, 2007 8:18 am
Posts: 370
Location: london
You need to give it the key object. If you've mapped properties of your entity as the key then your entity is also the key.
e.g.
Say User is your mapped object with properties fname and lname as the key. To load a user by pk you need to do this:
Code:
User userPk = new User();
userPk.setFname("kamilski");
userPk.setLname("81");
User loadedUser = session.get(User.class, userPk);


You should avoid composite Ids unless mapping to a legacy schema.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 14, 2007 1:34 pm 
Newbie

Joined: Tue Aug 14, 2007 2:46 am
Posts: 7
thank you. So when dealing with hibernate columns that define the table should not be a composite key? That kind of goes against ER database, correct?

Ie. I have a table where the three columns define its primary key, how should I change this to be hibernate complian then? create a unique_id and disregard the other three columns as beinga composite?

If I choose this solution I the .saveOrUpdate() will not function properly because the primary key is not defined properly? Could you possibly clarify this?


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 14, 2007 2:13 pm 
Expert
Expert

Joined: Fri Jul 13, 2007 8:18 am
Posts: 370
Location: london
Hibernate _can_ use composite keys its just the hibernate people think they are a bad idea. In the docs it says this for example:
Quote:
If you've fully embraced our view that composite keys are a bad thing and that entities should have synthetic identifiers (surrogate keys), ...


It certainly complicates your mappings and has some undesirable side effects, like the issuing of a select before an insert to determine if an object should be inserted or updated.

You should use a simple surrogate primary key like a Long for example, and map the other fields as properties. If you have data that should be unique you can still define a unique constraint across these fields to ensure data integrity. And of course, you can always query for instances using the fields that would have been in your composite pk.

saveOrUpdate works perfectly well with a surrogate key.


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