"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