Criteria API code that searches via associated Entity ID does not work in Hibernate 5.4.1. while it still worked in Hibernate 5.2.18,
This happens if the association is an optional OneToOne mapped with PrimaryKeyJoinColumn. If the association is mapped optional=false the query still works.
The cause seem to be the commit https://github.com/hibernate/hibernate-orm/commit/2bfe30009c15ea59c587d11714d4be78b14f9ad9 and the partial revert in https://github.com/hibernate/hibernate-orm/commit/8535adc05736e9ae93d818d9aff25bf9255a1bbd
The attached test case works with 5.2.18, but fails in 5.4.1.
AbstractPropertyMapping#hasNonIdentifierPropertyNamedId is incorrectly returning false when the associated entity ID is named id (as in the attached test).
Fixing AbstractPropertyMapping#hasNonIdentifierPropertyNamedId makes the test pass, but, the resulting query does not do a join with the associated entity table, which is incorrect for an optional one-to-one association.
Also, without fixing AbstractPropertyMapping#hasNonIdentifierPropertyNamedId, the generated SQL for the query "from Foo f where f.bar.id = :barId" properly has a join to the associated entity table.
After fixing AbstractPropertyMapping#hasNonIdentifierPropertyNamedId, the query does not have the join.
Interestingly, changing Bar#id to #id1 (or to anything other than id), the join no longer appears in the generated SQL for "from Foo f where f.bar.id = :barId", which is also a bug.
If/when this is fixed, we need to make sure that it does not cause a regression for JPA/HQL queries.
, this doesn't appear to be an issue in 5.3.9, so I removed "5.3+" from the title.
A workaround is to use an alias, as in:
needs to be fixed before this can be fixed.