after upgrade from 5.1.15 to 5.3.6 I observed one change related to getSingleResult() method's behaviour on some databases (some other DBs seem to be working OK, maybe it depends on dialect/driver).
In 5.1.15, when there were just duplicates in the result coming from DB, e.g. I was doing a projection on one attribute of type String, so the result from DB was for example
getSingleResult() method call was working in the way that it filtered out duplicates (using HashSet) and I got a single unique result, so no need for NonUniqueResultException, since result is unique (although duplicated) here.
Here is the code responsible for filtering out of duplicates:
However, with Hibernate 5.3.6 I am getting NonUniqueResultException in these scenarios. Although there is a filtering, it is done a little bit differently now. There is a uniqueElement method used in AbstractProducedQuery#getSingleResult() method:
Note this part:
I believe the cause of this different behaviour is that equals() method is not used there, rather "==" operator is, which means we compare here object's addresses, so in 99% of cases the condition will be true even though the content of the List would be just duplicates. If there was equals() my example would work as before on Hibernate 5.1.15, which was using HashSet, which in fact internally uses equals() as well.
Do you think this can be changed? I can help with a PR if you want, it looks like an easy change.
MS SQL 2014/2016