• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Property named id but not primary key

 
Rickard Sundin
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a Hibernate persisted class 'MyClass' with three properties.


When trying to lookup an instance with a specific 'id' using a HQL query, I run into trouble.


When using a property named 'id' in HQL, it is interpreted as a shortcut for whatever property Hibernate uses for primary key. In the documentation (11.7) I read: "The special property (lowercase) id may be used to reference the unique identifier of an object." In my case this behaviour effectively hides the "real" property 'id'.

How can I rewrite my HQL query so that it uses my property 'id'?

Best regards
/Rickard

PS. A workaround would be to provide my own SQL query, but I would like to solve this problem without leaving HQL.
[ September 01, 2004: Message edited by: Rickard Johansson ]
 
Gavin King
author
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
um ... rename the id property? ;-)
 
somkiat puisungnoen
Ranch Hand
Posts: 1312
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think, you can map id column with another name in xxx.hbm.xml persistent file.

<property name="yourID" column="id" type="string"/>


--------
ps. I'm not sure.
 
Rickard Sundin
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What I have a problem with is that Hibernate puts a restriction (although I admit that it is a rather small restriction) on how the properties of my business objects are named.

When building a new system, knowing from the start that Hibernate is being used, it is not a big deal to avoid using business meaning properties with the name 'id'. But in situations when migrating an existing system to Hibernate, or in when the business objects are modelled before the ORM implementation is chosen, you want to avoid unnecessary changes to the business objects.

/Rickard
 
Gavin King
author
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It would be incredibly trivial to patch Hibernate so it used some other name for the id property (a one line change) if this was ever a real problem.

Advantage of opensource stuff, you see...
 
Rickard Sundin
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why not make it a configuration property? If not stated, use 'id' as default alias, or state another name (or turn of the alias feature, which is what I would prefer).

When incorporating third party frameworks in systems with estimated long lifespan I try to do everything possible to avoid having to maintain a patched version of any third party framework. This means that I prefer getting invlolved in the development of an open source project (making the change a part of the official release) over maintaining a private patch.

/Rickard
 
Gavin King
author
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, no-one ever asked for it before
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic