72services / qlrm

Query Language Result Mapper

Home Page:http://qlrm.org

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

org.qlrm.mapper.JpaResultMapper cannot deal properly with unique results

JanMosigItemis opened this issue · comments

Thank you very much for this neat little mapping helper. Unfortunately I might have found a bug:

I have the following situation:

Query query = entityManager.createQuery("SELECT COUNT (e) FROM Entity e;");

This query will result in a uniqe Long as a result.
When calling jpaMapper.list(query, Long.class), this results in a ClassCastException.
This is also the case when calling jpaMapper.uniqueResult(query, Long.class).

The reason for this is that in the described situation

  • query.getResultList does return a List that contains a Long, not an Array of Long with one member.
  • query.getSingleResult does return a Long, not an Array of Long with one member.

A possible fix might be to post process the returned results so that it is guaranteed that a cast to Object[] will succeed.

@simasch @JanMosigItemis
I think is useless adding this cyclic procedure every time and for the entire result.
If the issue affects only the queries which returning one element, we can add this workaround to the post processing method:
if (rawResults.size() == 1) {
for (Object rawResult : rawResults) {
result.add(postProcessSingleResult(rawResult));
}
} else {
result = (List<Object[]>) rawResults;
}
What do you think?
Nicola

sounds resonable