FYI: using Hibernate 5.1
I have a use case where I need to filter a query by entity type, with 1..* entity types being selected.
So an example HQL would be, for selecting Cars related to User, but returning type CarTypeA (a subclass of Car) only:
Code:
SELECT DISTINCT b FROM com.zlatan.User a, com.zlatan.Car b WHERE a.id = b.user.id AND a.id = :param0 AND TYPE(b) in (com.zlatan.CarTypeA)
This query previously worked in a much older version of Hibernate (3.5.6) and the documentation for Hibernate 4 still claims to support this. However, since migrating to Hibernate 5.1 it has stopped working.
Using an in-memory Hsqldb the query returns 0 results when I would expect some.
Using an Oracle database the query actually throws an error: "ORA-00918: column ambiguously defined)"
The generated SQL roughly equates to:
Code:
SELECT DISTINCT
car1_.OBJECT_ID AS OBJECT_I1_190_,
car1_.NAME AS NAME3_190_,
car1_.clazz_ AS clazz_
FROM (SELECT
OBJECT_ID,
NAME,
105 AS clazz_
FROM USERS) user0_
CROSS JOIN (SELECT
OBJECT_ID,
NAME,
13 AS clazz_
FROM CARDTYPEA
UNION ALL
SELECT
OBJECT_ID,
NAME,
14 AS clazz_
FROM CARDTYPEB
UNION ALL
SELECT
OBJECT_ID,
NAME,
15 AS clazz_
FROM CARDTYPEC) car1_
WHERE user0_.OBJECT_ID = car1_.RACK_ID
AND user0_.OBJECT_ID = ?
AND (clazz_ IN (13))
I noticed in a previous version of Hibernate the "clazz_" mentioned in the where clause would usually have an alias, such as "carent1_.clazz_".
If I manually add the alias then the SQL will run fine. So it appears the HQL is not being converted correctly? (Or at least as I would expect previously).
Has this functionality been removed from Hibernate 5 or does this appear to be a bug?