# New query building in Hibernate 6

**URL:** <https://discourse.hibernate.org/t/new-query-building-in-hibernate-6/7283>\
**Category:** Hibernate ORM\
**Created:** [February 21, 2023, 7:59am UTC](https://discourse.hibernate.org/t/new-query-building-in-hibernate-6/7283 "2023-02-21T07:59:41Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![daromnik](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/daromnik/32/2254_2.png) [@daromnik](https://discourse.hibernate.org/u/daromnik)\
**Post date:** [February 21, 2023, 7:59am UTC](https://discourse.hibernate.org/t/new-query-building-in-hibernate-6/7283/1 "2023-02-21T07:59:41Z")

</div>

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:

```auto
public interface RoleRepository extends JpaRepository<Role, Long> {
    Role getByName(String roleName);
}

```

```auto
@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;
}

```

```auto
@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:

```auto
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:

```auto
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:

```auto
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:

```auto
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.

---

<div class="post-metadata">

**Author:** ![beikov](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/beikov/32/258_2.png) [@beikov](https://discourse.hibernate.org/u/beikov)\
**Post date:** [February 21, 2023, 9:00am UTC](https://discourse.hibernate.org/t/new-query-building-in-hibernate-6/7283/2 "2023-02-21T09:00:10Z")

</div>

This is a bug. Please create an issue in the issue tracker([https://hibernate.atlassian.net](https://hibernate.atlassian.net)) with a test case([hibernate-test-case-templates/JPAUnitTestCase.java at main · hibernate/hibernate-test-case-templates · GitHub](https://github.com/hibernate/hibernate-test-case-templates/blob/master/orm/hibernate-orm-6/src/test/java/org/hibernate/bugs/JPAUnitTestCase.java)) that reproduces the issue.

---

<div class="post-metadata">

**Author:** ![daromnik](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/daromnik/32/2254_2.png) [@daromnik](https://discourse.hibernate.org/u/daromnik)\
**Post date:** [February 21, 2023, 11:48am UTC](https://discourse.hibernate.org/t/new-query-building-in-hibernate-6/7283/3 "2023-02-21T11:48:05Z")

</div>

Done! [https://hibernate.atlassian.net/browse/HHH-16210](https://hibernate.atlassian.net/browse/HHH-16210)  
Thank you.
