New query building in Hibernate 6

Hello.

After update from Hibernate 5.6 to 6 (6.1.6 and 6.2.0) the construction of queries in hibernate 6 has changed.

I have repository:

public interface RoleRepository extends JpaRepository<Role, Long> {
    Role getByName(String roleName);
}
@Entity
public class Role {

    @Id
    private Long id;

    private String name;

    @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
    @JoinColumn(name = "roleName", referencedColumnName = "name")
    private List<RoleScopePermission> scopePermissions;
}
@Entity
public class RoleScopePermission {

    @Id
    private String roleName;

    @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
    @JoinColumn(name = "roleName", referencedColumnName = "roleName")
    @JoinColumn(name = "scopeName", referencedColumnName = "scopeName")
    private Set<RoleScopeOperationPermission> operationPermissions;

    @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
    @JoinColumn(name = "roleName", referencedColumnName = "roleName")
    @JoinColumn(name = "scopeName", referencedColumnName = "scopeName")
    private Set<RoleScopeObjectPermission> objectPermissions;

    @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
    @JoinColumn(name = "roleName", referencedColumnName = "roleName")
    @JoinColumn(name = "scopeName", referencedColumnName = "scopeName")
    private Set<RoleScopeOperationObjectPermission> operationObjectPermissions;
}

And when I used to make a method call getByName(String roleName) in console queries were displayed:

select role0_.* from public.roles role0_ where role0_.name=?
select scopepermi0_.* from public.role_scope_permissions scopepermi0_ where scopepermi0_.role_name=?
And a few queries:
select operationp0_.* from public.role_scope_operation_permissions operationp0_ where operationp0_.role_name=? and operationp0_.scope_name=?
select operationo0_.* from public.role_scope_operation_object_permissions operationo0_ where operationo0_.role_name=? and operationo0_.scope_name=?
select objectperm0_.* from public.role_scope_object_permissions objectperm0_ where objectperm0_.role_name=? and objectperm0_.scope_name=?

And in the end the result was like this:

result = {Role@27025} :
 id = {Long@27028} 3
 name = "INTER"
 scopePermissions = {PersistentBag@27030}  size = 3
 [
	0 = {RoleScopePermission@27042} 
		roleName = "INTER"
		operationPermissions = {PersistentSet@27048}  size = 0
		objectPermissions = {PersistentSet@27049}  size = 0
		operationObjectPermissions = {PersistentSet@27050}  size = 18
	1 = {RoleScopePermission@27043} 
		roleName = "INTER"
		operationPermissions = {PersistentSet@27079}  size = 1
		objectPermissions = {PersistentSet@27080}  size = 0
		operationObjectPermissions = {PersistentSet@27081}  size = 18
	2 = {RoleScopePermission@27044} 
		roleName = "INTER"
		operationPermissions = {PersistentSet@27084}  size = 0
		objectPermissions = {PersistentSet@27085}  size = 0
		operationObjectPermissions = {PersistentSet@27086}  size = 1
  ]

After update to Hibernate 6:

select r1_0.* from public.roles r1_0 where r1_0.name=?
 
select s1_0.* , o2_0.* , o3_0.* 
from public.role_scope_permissions s1_0 
left join public.role_scope_object_permissions o1_0 on s1_0.role_name=o1_0.role_name and s1_0.scope_name=o1_0.scope_name 
left join public.role_scope_operation_object_permissions o2_0 on s1_0.role_name=o2_0.role_name and s1_0.scope_name=o2_0.scope_name 
left join public.role_scope_operation_permissions o3_0 on s1_0.role_name=o3_0.role_name and s1_0.scope_name=o3_0.scope_name 
where s1_0.role_name=?

And the result became like this:

result = {Role@26448} :
 id = {Long@26463} 3
 name = "INTER"
 scopePermissions = {PersistentBag@26465}  size = 37
  0 = {RoleScopePermission@26503} 
  1 = {RoleScopePermission@26503} 
 ...
  35 = {RoleScopePermission@26504} 
  36 = {RoleScopePermission@26505} 

It turned out duplication of RoleScopePermission by the number of operationObjectPermissions (18 + 18 + 1).

How can I fix this behavior with settings or is it a bug?

Sorry for a lot of text, I simplified it as best I could.
Thank you.

This is a bug. Please create an issue in the issue tracker(https://hibernate.atlassian.net) with a test case(hibernate-test-case-templates/JPAUnitTestCase.java at main · hibernate/hibernate-test-case-templates · GitHub) that reproduces the issue.

Done! https://hibernate.atlassian.net/browse/HHH-16210
Thank you.