-->
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.  [ 2 posts ] 
Author Message
 Post subject: Special HQL keyword 'id' breaks my JPA queries
PostPosted: Thu Jan 18, 2007 9:23 am 
Newbie

Joined: Tue Jan 03, 2006 4:24 am
Posts: 4
"The special property (lowercase) id may be used to reference the
unique identifier of an object. (You may also use its mapped
identifer property name.). Note that this keyword is specific to
HQL."
http://www.hibernate.org/hib_docs/entitymanager/reference/en/html/queryhql.html

This breaks querying for my entity that has a property called id which is not the primary key of the entity.

The reason for this property is that my entity has a surrogate key that is not named id.

Is there a workaround for this "bug"?

Example entity and tests that failes:
Code:
@Entity
public class Foo {
   @Id
   private int primaryKey;
   
   private String id; // not primary key, but unfortunatly named id

   public Foo(int primaryKey, String id) {
      this.primaryKey = primaryKey;
      this.id = id;
   }

   public String getId() {
      return id;
   }

   public int getPrimaryKey() {
      return primaryKey;
   }
}
...
public class HibernateTest extends TestCase {
   private EntityManager em;

   private Foo foo;

   protected void setUp() {
      em = Persistence.createEntityManagerFactory("hibernatebug")
            .createEntityManager();
      foo = new Foo(1, "A");
      em.getTransaction().begin();
      em.persist(foo);
   }

   public void testSpecialIdKeyword() {
      Query query = em.createQuery("select f from Foo f " +
            "where f.id = :param");
      query.setParameter("param", 1);
      try {
         assertEquals(foo, query.getSingleResult());
         fail("this should case ClassCastException, " +
               "and definitely not pass");
      } catch (ClassCastException e) {}
   }

   public void testCannotQueryForIdField() {
      Query query = em.createQuery("select f from Foo f "
            + "where f.id = :param");
      query.setParameter("param", "A");
      try {
         assertEquals(foo, query.getSingleResult());
      } catch (ClassCastException e) {
         fail("this should not cause ClassCastException");
      }
   }

   protected void tearDown() throws Exception {
      em.getTransaction().rollback();
   }
}


Hibernate version:
3.2.1.ga


Top
 Profile  
 
 Post subject:
PostPosted: Sun Jan 21, 2007 8:54 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
I believe this will be relaxed in 3.2.2, check JIRA

_________________
Emmanuel


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