Relations are not loaded when using Fetch Profiles

Description

When you use Fetch Profiles to override relations lazy loading, some relations are not loaded, they still behave like lazy.

When a SQL select is constructed to load data, here the active fetch profiles are considered and additional tables are joined according to fetch profiles. But when you need to load some additional data in TwoPhaseLoad class, here active fetch profiles are not considered and it bahaves always like lazy relation and data are not loaded.
There are some examples below, these examples are also in tests in my commit.

Example 1

Imagine bidirectional ManyToMany relation between classes A and B. They are set as eager from both sides. When I load an instance A, Hibernate also loads related B instances and from them loads related A instances and again and again until all instances in the graph are initialized.
But when relations are set as lazy and I override them by fetch profile only first SQL select is executed and other enties are not initialized.

Example 2

Imagine following classes:

Relations are unidirectional, from left to right and are always ManyToOne.
Imagine following instances in runtime:

So all classes have just one instance only class Via1 has no instance.
If I load entity Start and all relations are eager, Hibernate load all entities. But if relations are lazy and overridden by fetch profile, the instance Finish is missing.
The reason is the relation Mid -> Finish is not incorporated to the SQL select because the relation is detected as cycle (because it is found second time, first time was found when it went via Via1) and TwoPhaseLoad class, which would load it additionaly, ignores fetch profile.

Environment

Tested on Postgre SQL 9.6, and HSQLDB

Assignee

Unassigned

Reporter

Ladislav Kulhánek

Fix versions

Labels

None

backPortable

Backport?

Suitable for new contributors

None

Requires Release Note

None

Pull Request

None

backportDecision

None

Components

Affects versions

Priority

Major
Configure