Exception when using the Treat-Operator with MapJoins


I am experiencing some buggy behaviour regarding the TREAT operator on MapJoins. In the following I will only sketch the scenario, for the full detail see the attached maven project.

I have an entity

that holds a mapping from keys (entities) to values (also entities). DBValue is an abstract super class for several subclasses that store a specific value, e.g. DBValueWithString or DBValueWithReference (which references other DBObjects).

I now want to query for objects, that own specific properties with certain values. Therefore I use the following CriteriaQuery:

This query generates to reasonable JPQL:

The corresponding SQL seems a little off, however it works:

Note the unnecessary join with DBValueWithReference. According to the doc, the TREAT operator should also filter out the non matching subtypes, therefore entities of type DBValueWithReference can be completely ignored at this point.

The problem now arises, when querying for DBValueWithReferences. The JPQL query seems fine once again:

However the translation process to SQL fails with the following error message:

The problem seems to correlate with the unnecessary join, as the type seems to be inferred solely based on the first join (with DBValueWithString), which is unnecessarily part of the second query.

It seems, DBValue is joined with every sub class that is encountered in the persistence.xml. If you uncomment the @Entity annotation for DBValueWithBoolean, it will occur in the list of join statements.

Moreover, the order of the joins seems to depend on the order of which the classes are listed in the persistence.xml. If you swap the entries for DBValueWithString and DBValueWithReference, the second query executes just fine, whereas the first query now yields:

So is this just a misinterpretation of what the TREAT operator is supposed to do, or is it actually a bug in the SQL generation?


To further elaborate on this issue, I tried to realize the same scenario using the EclipseLink JPA provider. Thereby I realized, the code given in the example may be a little unclean. For example, I used the same Join-object in several calls to the treat function. Apparently, this function may alter the Join-object, which may cause the query to fail. I added a new test project, which hopefully gets rid of all these flaws.

Ultimately, the new test-case allows a direct comparison of Hibernate and EclipseLink (via maven profiles) which shows, that the code executes as expected using EclipseLink but fails when using Hibernate. The main issue seems to be the same as mentioned above: When performing the TREAT operation, Hibernate wrongly joins on every subtype of the superclass.


Linux, Java8, HSQLDB & Postgres


Gail Badner
October 10, 2017, 5:24 AM

May be related to HHH-10759. Both of these jiras involve JOINED inheritance with 2 subclasses that have a property with the same name.





Fix versions





Suitable for new contributors


Requires Release Note


Pull Request





Affects versions