-->
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: Puzzled. How did HQL reference object id?
PostPosted: Tue Sep 05, 2006 11:08 pm 
Newbie

Joined: Tue Jan 06, 2004 8:06 am
Posts: 13
Hi, recently I reviewed my code and something puzzled me quite a bit.

In my HQL, I have something like
"select..... from eg.Employee e .... where e.id=?"

All seems normal to me until I realized in my eg.Employee bean I never declared any id field. The primary key is userId, likewise the same for the hbm.xml which maps userId to USER_ID.

The query runs fine (I get the same result using e.userId or e.id in the HQL).

So do Hibernate automatically maps object.id to the primary key of the table?

I am currently using 2.1.8, sorry I have to post this since the topic is so vague (to me at least) I am not able to find anything relevant in the forum.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Sep 06, 2006 2:35 am 
Beginner
Beginner

Joined: Mon Nov 22, 2004 11:21 am
Posts: 42
The mapping for your Employee class contains the tag
Code:
<id name="id" type="....

If you choose a different name, you have to refer to the new name for the id property.

Regards,
Georg

_________________
Real programmers confuse Halloween and Christmas because OCT 31 = DEC 25


Top
 Profile  
 
 Post subject:
PostPosted: Thu Sep 07, 2006 12:56 am 
Newbie

Joined: Tue Jan 06, 2004 8:06 am
Posts: 13
The mapping is as follow
Code:
<hibernate-mapping>
   <class name="eg.Employee" table="EG_EMPLOYEE">
      <id column="USER_ID" name="userId" type="string" length="32">
          <generator class="assigned"/>
      </id>   

      <property column="NAME" name="name"  type="string" length="64"/>
...
  </class>     
</hibernate-mapping>



The bean object is as follow
Code:
public class Employee {

  /** identifier field */
  private String userId;
...
  public String getUserId() { return userId;};
  public void setUserId(String uid) { this.userId = uid;}
}


Is it due to how we have identified USER_ID as id in hbm.xml and hence in HQL "employee.id" will always referenced that field?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Sep 07, 2006 2:06 am 
Beginner
Beginner

Joined: Mon Nov 22, 2004 11:21 am
Posts: 42
Now you got me puzzled too. ;-) In such cases I read the reference manual more carefully. Indeed, in chapter 14.8 it says: "The special property (lowercase) id may be used to reference the unique identifier of an object. (You may also use its property name.)"

Regards,
Georg

P.S.: Please vote if this information is helpful

_________________
Real programmers confuse Halloween and Christmas because OCT 31 = DEC 25


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.