When selecting on a disjunction of a property in a base class and a property in a subclass (treat), the restriction on the dtype is placed incorrectly causing on the matches of the subclass to be returned. Similar behavior was already present in older versions of Hibernate ORM (tested in 5.3.28 and 5.6.14), however it seems 6.1.5 is more consistent in rendering the incorrect restriction, which causes queries in our application to miss results. In 5.3.28 these queries did return results because the dtype restriction was missing entirely (which also is incorrect). In the example of a query rendered by 6.1.5 below, it is clear that this can only return records of type PLDAPDirectory:
select
p1_0.id,
p1_0.DTYPE,
p1_0.active,
p1_0.openldap
from
(select
*
from
PAccountDirectory t
where
t.DTYPE='PLDAPDirectory') p1_0
where
p1_0.active=?
or case
when p1_0.DTYPE='PLDAPDirectory' then p1_0.openldap
else null
end=?
Corresponding Java code:
CriteriaBuilder cb = entityManager.getCriteriaBuilder();
CriteriaQuery<PAccountDirectory> query = cb.createQuery(PAccountDirectory.class);
Root<PAccountDirectory> root = query.from(PAccountDirectory.class);
From<?, PLDAPDirectory> ldap = cb.treat(root, PLDAPDirectory.class);
Predicate exp = cb.or(cb.equal(root.get("active"), true),
cb.equal(ldap.get("openldap"), true));
entityManager.createQuery(query.select(root).where(exp)).getResultList();
|