We're updating the issue view to help you get more done. 

Join is reused when 2 explicit joins are used for the same ToOne association

Description

The bug was introduced by HHH-9305.

Example:
Tree entities with OneToOne association:
person <> address <> country

We are using JPA criteria that's why we cannot use fetch in the where clause. So we are doing the following:

1 2 3 4 5 6 7 select persion from Persion as persion inner join persion.address as address_filter inner join fetch persion.address as address inner join fetch address.country where address_filter.city_name = 'some value';

After changes it reuses the first fetch and query (during parsing) is become as this:

1 2 3 4 5 6 select persion from Persion as persion inner join persion.address as address_filter inner join fetch address.country where address_filter.city_name = 'some value'

Because it cannot found parent for country fetch it throws an error:
org.hibernate.QueryException: query specified join fetching, but the owner of the fetched association was not present in the select list

Since I cannot imagine a case where I need to do join twice in OneToOne associations except for fetching I think it's ok to reuse join here but for this case it should be the second join (with fetch option).

I have found a good workaround.

1 2 3 4 5 6 select persion from Persion as persion inner join fetch persion.address as address inner join fetch address.country where persion.address.city_name = 'some value';

This query can be converted to JPA criteria query and will do exactly the same thing without any errors.

Environment

None

Status

Assignee

Gail Badner

Reporter

Oleksandr Podoprygora

Fix versions

Labels

None

backPortable

None

Suitable for new contributors

None

Requires Release Note

None

backportDecision

None

Components

Affects versions

4.2.16

Priority

Minor