Make CriteriaImpl.uniqueResult() more robust

Description

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.

Environment

None

Activity

Show:
Carlos Yagüe
September 13, 2017, 10:03 AM
Edited

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:

Gail Badner
September 19, 2017, 12:25 AM

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.

[1] http://docs.jboss.org/hibernate/orm/5.2/userguide/html_single/Hibernate_User_Guide.html#appendix-legacy-criteria

Carlos Yagüe
September 19, 2017, 4:42 PM

Hi!

this issue is occurring using HQL:

Nowadays, I'll consider the fact of Hibernate 3.x-5.1 are no longer mantained.

Regards

Assignee

Unassigned

Reporter

TomasT

Fix versions

None

Labels

backPortable

None

Suitable for new contributors

None

Requires Release Note

None

Pull Request

None

backportDecision

None

Components

Affects versions

Priority

Major