-->
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.  [ 3 posts ] 
Author Message
 Post subject: Column Named ID is not Primary Key - SQL error
PostPosted: Wed Dec 13, 2006 10:16 am 
Newbie

Joined: Wed Dec 13, 2006 9:54 am
Posts: 5
Hello everyone,

i have a tiny problem that puzzles me all day long :

Java Object :
/**
* @return the localKey
* @hibernate.id generator-class="increment" column="localKey"
*/
public String getLocalKey(){
return localKey;
}
/**
* @return the id
* @hibernate.property column="id"
*/
public String getId() {
return id;
}
---
as you can see, the primaryKey of the Database Table is within the column localKey, BUT there is a 2nd column named ID, which is NOT the PK.

ok, the hbm.xml file snippet for this looks like this :
<id name="localKey"
column="localKey"
type="java.lang.String">
<generator class="increment"></generator>
</id>
<property name="id"
type="java.lang.String"
update="true"
insert="true"
column="id" />

-----------------
The Criteria Query to get some Data looks like this :
Criteria query= session.createCriteria(c);
query.add(Restrictions.like("id",id));
----------------
The generated SQL String looks like this :
select .... from myTable where this_.localKey like ?

-------------------------------------------------------------
Am I using some sort of reserved propertyname by using "id" ?
Somehow Hibernate translates my Restriction "id" into "localKey" and I cant figure out why :(


Top
 Profile  
 
 Post subject:
PostPosted: Wed Dec 13, 2006 12:56 pm 
Expert
Expert

Joined: Tue Jul 11, 2006 10:21 am
Posts: 457
Location: Columbus, Ohio
Yes, id is a reserved word which normally is a shortcut way of referencing the primary key of the table.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Dec 13, 2006 3:06 pm 
Newbie

Joined: Wed Dec 13, 2006 9:54 am
Posts: 5
Thanks for the reply !

I changed the objects property from "id" to "myID", and left the mapping point at the "id"-column in the database table - works fine now.

Still ... i cant really see much good coming out of this "feature" that a property named "id" automatically points at the primary key.

Especially in my kinna situation, where u have a legacy system running onto the database as well, so you cant change column names etc.

anyhow, works great now - hibernate at its best :)


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