We are using Sybase for generating a query that has a COUNT and ORDER BY. Granted, the 'order by' doesn't have any semantic effect, but it's a dynamically generated query, and we need to know the total count as well as the actual result.
The problem is that Sybase returns many rows for such a query, and the uniqueResult() fails with a NonUniqueResultException. Although every returned row contains an Integer with the same value, they are different instances.
We could modify the AbstractQueryImpl.uniqueElement(List list) as follows:
static Object uniqueElement(List list) {
int size = list.size();
if (size == 0) return null;
Object first = list.get(0);
for (int i = 1; i < size; i++) {
if (!safeEquals(first, list.get)) {
throw new NonUniqueResultException(list.size());
}
}
return first;
}
static boolean safeEquals(Object first, Object second) {
if (first instanceof Integer && second instanceof Integer) {
return ((Integer)first).compareTo((Integer)second) == 0;
}
return first == second;
}
That would solve our issue.
Thanks.
Hi!
I'm agree with @tpollak. The implementation of method uniqueElement is wrong. Using operators == or != isn't valid to compare object because each row has a different instance. It must be implemented using equals(). Like this way:
Hibernate 3.x, 4.x, and 5.0 are no longer maintained. Shortly, 5.1 will no longer be maintained. In addition org.hibernate.internal.CriterialImpl is considered deprecated. [1]
Please verify that your issue is reproduces using HQL or JPA CriteriaQuery in 5.2.
Hi!
this issue is occurring using HQL:
Nowadays, I'll consider the fact of Hibernate 3.x-5.1 are no longer mantained.
Regards