-->
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.  [ 2 posts ] 
Author Message
 Post subject: Hibernate identifier using non-primarytable columns?
PostPosted: Mon Nov 07, 2005 5:22 pm 
Newbie

Joined: Mon Nov 07, 2005 5:10 pm
Posts: 1
Hi All,

As far as I know, all the query methods inside session object would query DB records thru identifier and this identifier is mapped to primary key of the table. Is it possible to define this identifier to table columns other than primary key in mapping file? I'm aware of I can always use HQL/SQL to retrieve them but using identifier would avoid writing those HQL/SQL statements.
Thanks a lot.

Jason Li
Computer Associates.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Nov 07, 2005 10:44 pm 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
Hibernate does not force a relationship between the id and the primary key in the table. It's a very very good idea to have the id and primary key be the same, but theoretically you can work around it. (In fact hibernate doesn't even force you to have a primary key at all, though it does force you to have an id) Just avoiding a little HQL code or an extra factory method or two would not qualify, in my mind, as a sufficient reason to break the id/primary key relationship.

But if you think it's best: just use assigned as your id generator strategy and set the id to the appropriate value, which will have to be unique per row. For your real DB primary key, which will presumably be DB-generated (default value in the DB, identity type column, whatever), make sure that you make it a read-only column in your mapping, using insert="false" and update="false". You could generate the primary key yourself in your save logic, and leave insert="true", but then you're beginning to delve into the depths of DB hackery.

Again, let me urge you to go with the HQL query options, or at least use Criteria. session.get(X.class, id) isn't that much easier than createQuery("X where column = :value").setString("value").uniqueResult().


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