I have the following hibernate query:
public long countHouseBoats(HouseBoat houseBoat, Type type, List<Long> excludedIds) {
TypedQuery<Long> query = entityManager.createQuery("SELECT COUNT(o) FROM AquaticCity o " +
"WHERE o.houseBoat = :houseBoat " +
"AND o.chimneyType = :chimneyType " +
"AND o.keel.manufacturer.id NOT IN (:excludedIds) " +
"AND o.type.name IN (:types)", Long.class);
query.setParameter("houseBoat", houseBoat);
query.setParameter("chimneyType", ChimneyType.REGULAR);
query.setParameter("excludedIds", excludedIds);
query.setParameter("types", toStringList(List.of(type)));
return query.getSingleResult();
}
It works as I expect if excludedIds
is not empty. If it is empty then it is as though NOT IN <emptyList>
evaluates to false
rather than always evaluating to true
as one would expect.
I am out of ideas why this happens. I have read around and some similar questions exists, but they are not really the same.
I'm about to resort to the obvious ugly workaround of checking if the list is empty and if it is, build the query without the excludedIds
check.