Affects Version/s: 5.0.0.Final, 5.1.0, 5.0.8, 5.2.0
It seems that the handling of JPQL KEY() functions has changed in Hibernate 5.1.0.Final in comparison to Hibernate 4.3.8.Final.
I have attached a test project that illustrates the issue. Following JPQL query is used (templateEntries is a Map<Template, TemplateEntry>, both entities):
Hibernate 5.1.0.Final generates the following query:
Hibernate 4.3.8.Final generates the following query:
So Hibernate 5 effectively fetches the key whereas Hibernate 4.3.x only selects the id of the key.
In my case, the behavior of Hibernate 5 causes an issue due to the inner join introduced which effectively dumps all documents without templateEntries (which I specifically aimed to prevent by using left joins). Obviously, this use case worked with Hibernate 4.3.x.
Changing the inner join to a left join would fix this concrete case.
I had a quick look in the spec and it seems that it does not define whether the use of KEY() should fetch the key or not, though the Hibernate 5 way feels "more right". However, it would be nice to have a way to only select the id of a key and thereby eliminating the need for the fetch join by just selecting templateen2_.templateEntries_KEY.
This could be achieved by allowing both KEY(templateEntries) and KEY(templateEntries).id. Hibernate currently does not support dereferencing of KEY() functions but the spec contains examples of similar usages.