-->
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.  [ 10 posts ] 
Author Message
 Post subject: querying for the property named 'id'
PostPosted: Tue Feb 21, 2006 9:36 am 
Newbie

Joined: Mon Dec 05, 2005 8:13 am
Posts: 9
I have an entity that has property named id that is not a primary key.

Code:
class Example
{
  @Id
  Integer getKey();

  @Basic
  Integer getId();
}


I need to query for an object with a given id property (say: id=5) and I do it like this.

Code:
manager.createQuery( "select e from Example e where e.id = :id").setParameter( "id", 5).getResultList();


But this query will return an object with property key = 5. I found in docs that keyword id in HQL always means entity's primary key. But how to write query that uses id property that is not a key?


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 21, 2006 10:38 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
id is a keyword in HQL because Hibernate support more features than EJB3.
We'll need to fix that regarding EJB3.

Note that calling a non id property "id" while having an id property not called id is a bit schizophrenic and errorprone.

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 21, 2006 11:05 am 
Newbie

Joined: Mon Dec 05, 2005 8:13 am
Posts: 9
emmanuel wrote:
Note that calling a non id property "id" while having an id property not called id is a bit schizophrenic and errorprone.


I agree, but this is an extraordinary case: I must implement some interface which defines method 'getId', so I cannot rename it.

Is there any way to rewrite my query in hql to return the results I'm expecting?

I know I can write a native sql query that would return the real id and then fetch my entity with hql, but I don't like this solution.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 21, 2006 12:10 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
Not with HQL, sorry.
Try CriteriaQuery or Native query

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 21, 2006 12:41 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
so this is totally untested, but might work with a little known feature called "noop":

Code:
<id>...</id>
<property name="id" column="theidcolumn"/>
<property name="alternativeId" access="noop" column="theidcolumn" insert="false" update="false"/>


this *might* allow you to do:

Code:
manager.createQuery( "select e from Example e where e.alternativeId = :id").setParameter( "id", 5).getResultList();


it is a *workaround* that should only be used until we get the "id" hiding feature solved in the ejb3 handling.

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 21, 2006 3:06 pm 
Newbie

Joined: Mon Dec 05, 2005 8:13 am
Posts: 9
OK, I see, but is it possible to do it with Annotations?
I don't see any property like 'access' in @Column

BTW, I solved the problem with native query. Thanks for your time
Pawel


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 21, 2006 5:39 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
no, not possible with annotations afaik.

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 21, 2006 7:49 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
This is doable through annotations

@AccessType("noop")
@Column(name="id", insertable=false, updatable=false)
private property String getAlternativeId()

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 22, 2006 1:58 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
wow there is a hack ;) fun.

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 22, 2006 6:09 am 
Newbie

Joined: Mon Dec 05, 2005 8:13 am
Posts: 9
Just for the record: I tested the solution with @AccessType and it does work. Thanks


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