-->
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.  [ 5 posts ] 
Author Message
 Post subject: HQL generates SQL with mismatched column name?
PostPosted: Mon Jun 28, 2004 12:31 pm 
Newbie

Joined: Tue Sep 09, 2003 3:39 am
Posts: 14
Location: Atlanta, GA
I have a hibernate id named "hid". It is a Long. I have another column named "id" that I use in my app. It is a String. It seems that HQL is incorrectly mapping the column named "id" to the hibernate id column "hid" in the SQL that it generates.

That is, this code:

-----
// DataMapper is a convenience class that handles threadlocal sessions, etc.
DataMapper mapper = DataMapper.makeDataMapper();
System.out.println( mapper.find( "from Package p where p.id = 'default-cs'" ) );
-----

generates this query:

-----
select package0_.hid as hid, package0_.id as id, package0_.name as name, package0_.service as service from packages package0_ where (package0_.hid='default-cs' )
-----

which gives this exception:

-----
net.sf.hibernate.JDBCException.<init>(JDBCException.java:38)
ERROR: Could not execute query
java.sql.SQLException: ERROR: Bad int8 external representation "default-cs"
-----

because the where clause in the SQL is wrong: "package0_.hid" should be "package0_.id"

If I rename my "id" column to anything else, say "xxxid", the where clause is correct. This error only happens when my id column is named "id".

Relevant part of mapping file:

-----
<hibernate-mapping>
<class
name="com.borderless.data.Package"
table="packages"
dynamic-update="false"
dynamic-insert="false"
>

<id
name="hid"
column="hid"
type="java.lang.Long"
>
<generator class="sequence">
<param name="sequence">packages_hid_seq</param>
</generator>
</id>

<property
name="id"
type="java.lang.String"
update="true"
insert="true"
column="id"
not-null="true"
/>

...

</class>
</hibernate-mapping>
-----

Versions:
Hibernate 2.1.1
PostgresSQL 7.3.4
Java 1.4.2_04

Ideas?

--
John Lindsey


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jun 28, 2004 12:42 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
id is a keyword in HQL, sorry. You're the first guy I know who has the problem

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jun 28, 2004 1:52 pm 
Newbie

Joined: Tue Sep 09, 2003 3:39 am
Posts: 14
Location: Atlanta, GA
Thanks for the quick response!

Interesting, I would have never guessed that "id" would be a reserved word.

Just out of curiosity, is there a simple reason for having it always map to whatever your hibernate id column is?

Hmm, if I'm the first person to notice this, I wonder if I'm doing something odd. All of my hibernatable objects have an application id--I never explicitly touch the hibernate id in the app. So, for the sake of orthogonality, I thought I would rename all of my various appids to "id" (instead of "packageId", "objectxId", "objectyId", etc.). I could of course still do this, I just have to pick a name other than "id". :)

--
John Lindsey


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jun 28, 2004 3:52 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
the only thing you have to do is not using the name 'id' for a non id property in the Java world.
I've thought about this keyword a while ago and found a reason. But I can't remember now ;-)

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jun 29, 2004 12:54 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
The reason is that the identifier property is actually optional. This is kinda legacy, all classes should define identifier properties, I think.


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