0

Below piece of code is supposed to return list of entities for which the primarykeys(idList) match in DB.

This works fine for primitive Id types, but for composite keys, it does not. please help me.

Class<?> entityType = TypeToken.of(typeOfEntity).getRawType();
Class<?> idType = TypeToken.of(typeOfId).getRawType();

String idFieldName = getIdFieldName(entityManager, entityType, idType);

CriteriaQuery<?> criteria = entityManager.getCriteriaBuilder().createQuery(entityType);
Root<?> root = criteria.from(entityType);
Expression<ID> expression = root.get(idFieldName);
Predicate predicate = expression.in(idList);

TypedQuery<?> query = entityManager.createQuery(criteria.where(predicate));
return (List<T>) query.getResultList();

I am using Hibernate now, and when i run the test i got the following sql from hibernate logs.

select * from paper_invoice paperinvoi0_ where paperinvoi0_.paymentid=? and paperinvoi0_.receipt_code=? and paperinvoi0_.usetype=?

This looks fine, but it throws an exception: javax.persistence.PersistenceException: org.hibernate.exception.GenericJDBCException: could not execute query.

I am not sure whats the issue here. can anyone point out ?

1 Answers1

0

There is a bug in Eclipselink that produces incorrectly substituted query like this:

SELECT ID, TITLE FROM BOOK WHERE ((NULL, NULL) IN ((1, 2), (2, 2)))

which fails with Syntax error: Encountered "NULL". With Hibernate correct query is produced

select book0_.id as id1_0_, book0_.title as title2_0_ from Book book0_ where book0_.id=? and book0_.title=? or book0_.id=? and book0_.title=?

See also this question.

Community
  • 1
  • 1
zbig
  • 3,830
  • 2
  • 29
  • 37
  • now that you mentioned this as bug, can you please point me to the bug (bug id or bug url)? – Krishna Teja Sep 03 '14 at 02:56
  • I'm sorry. I was looking but didn't found a bug report for this issue. For me it's obviously a bug. If you are kind enough you can issue a bug here https://bugs.eclipse.org/bugs/enter_bug.cgi. – zbig Sep 03 '14 at 07:58