Hibernate - niepoprwane zapytania

0

Mam pytanie bo wykonując zapytanie query createQuery(SELECT u from User u)
dostaje coś takiego w konsoli(chyba nie powinno tak być?), przy większej ilości zapytań kiedy mam kilkadiesąt user-ów to zapytanie trwa około 200ms. Czym to jest spowodowane?

Hibernate: select user0_.id as id1_2_, user0_.email as email2_2_, user0_.first_name as first_name3_2_, user0_.last_name as last_name4_2_, user0_.password as password5_2_ from user_details user0_
Hibernate: select roles0_.user_id as user_id1_3_0_, roles0_.role_id as role_id2_3_0_, role1_.id as id1_1_1_, role1_.name as name2_1_1_ from users_roles roles0_, role role1_ where roles0_.role_id=role1_.id and roles0_.user_id=?
Hibernate: select events0_.user_id as user_id6_0_0_, events0_.id as id1_0_0_, events0_.id as id1_0_1_, events0_.created as created2_0_1_, events0_.description as description3_0_1_, events0_.location as location4_0_1_, events0_.name as name5_0_1_, events0_.user_id as user_id6_0_1_ from Event events0_ where events0_.user_id=?
Hibernate: select roles0_.user_id as user_id1_3_0_, roles0_.role_id as role_id2_3_0_, role1_.id as id1_1_1_, role1_.name as name2_1_1_ from users_roles roles0_, role role1_ where roles0_.role_id=role1_.id and roles0_.user_id=?
Hibernate: select events0_.user_id as user_id6_0_0_, events0_.id as id1_0_0_, events0_.id as id1_0_1_, events0_.created as created2_0_1_, events0_.description as description3_0_1_, events0_.location as location4_0_1_, events0_.name as name5_0_1_, events0_.user_id as user_id6_0_1_ from Event events0_ where events0_.user_id=?

//User.java
 @OneToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
 @JoinColumn(name = "user_id")
 private Set<Event> events;

 @ManyToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
 @JoinTable(
          name = "users_roles",
          joinColumns = @JoinColumn(name = "user_id", referencedColumnName = "id"),
          inverseJoinColumns = @JoinColumn(name = "role_id", referencedColumnName = "id")
  )
 private Collection<Role> roles;

Role i > Event to tylko klasy entity, z kilkoma polami string bez żadnych oneToMany czy manyToMany

[
    {
        "events": [],
        "firstName": "kjghkgh",
        "id": 141,
        "roles": [
            {
                "id": 142,
                "name": "ROLE_USER"
            }
        ]
    },
    {
        "events": [],
        "id": 143,
        "roles": [
            {
                "id": 144,
                "name": "ROLE_USER"
            }
        ]
    }
]
0

mam pytanie, czy w moim przypadku powinienem mieć tylko jedno select w konsoli ? cos w rodzaju select user0_.id ....from userdetails user0

1

Odkryłeś problem n+1.

Jeżeli masz zapytanie, którym chcesz pobrać wszystkich userow, a userzy maja jakaś kolekcje to najbardziej optymalnym rozwiązaniem jest zrobienie jednego tripa do bazy danych, czyli powinieneś mieć docelowo jedno zapytanie, a nie jak teraz.

Masz kilka sposobów by to rozwiązać, dostałeś linka z instrukcjami jak to zrobić.

Od siebie dodam, ze najlepszym jest fetchMode ustawiony na subSelect, bo zachowujesz lazyLoading, nie ma problemu kartezjanskiego, n+1 nie występuje.

Dodatkowo, gdy pobierasz duzo userow to co? Zapcha ci się system. Powinieneś rozważyć paginacje w tym przypadku, by porcjami pobierać dane.

0

dziękuje za odpowiedzi

1 użytkowników online, w tym zalogowanych: 0, gości: 1