简体   繁体   中英

Hibernate keeps creating N+1 querys

I've got no idea why is this happening. I thought the "JOIN FETCH" thing and/or "EntityGraph" were meant to address this, but then the query made by Hibernate looks even more uglier. Here's my code:

Entity class:

package com.udemy.springgraphql.jpa.model;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.hibernate.annotations.Type;

import javax.persistence.*;
import java.time.LocalDateTime;
import java.time.OffsetDateTime;
import java.util.UUID;

@Entity
@Table(name = "review")
@Data
@AllArgsConstructor
@NoArgsConstructor
@Builder
public class Review {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO, generator = "UUID")
    @Type(type = "uuid-char")
    private UUID id;

    private String author;
    private String description;
    private int rating;
    private OffsetDateTime published;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "wadId")
    private Wad wad;

}

Repository class:

package com.udemy.springgraphql.jpa.repository;

import com.udemy.springgraphql.jpa.model.Review;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.EntityGraph;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Set;
import java.util.UUID;

@Repository
public interface ReviewRepository extends JpaRepository<Review, UUID> {

    @Query("SELECT r FROM Review r JOIN FETCH r.map where r.id in (:ids)")
    @EntityGraph(attributePaths = {"map"})
    List<Review> findAllByIdPlusMaps(Set<UUID> ids);

}

And then the logs:

2022-04-29 10:03:16.768  INFO 27716 --- [pool-2-thread-1] c.u.s.service.impl.MapServiceImpl        : Starting query...
2022-04-29 10:03:16.780 DEBUG 27716 --- [nPool-worker-27] org.hibernate.SQL                        : select wad1_.id as id1_2_, wad1_.download_link as download2_2_, wad1_.genre as genre3_2_, wad1_.iwad as iwad4_2_, wad1_.name as name5_2_, wad1_.released as released6_2_ from review review0_ inner join wad wad1_ on review0_.wad_id=wad1_.id where review0_.id=?
2022-04-29 10:03:16.780 DEBUG 27716 --- [nPool-worker-23] org.hibernate.SQL                        : select wad1_.id as id1_2_, wad1_.download_link as download2_2_, wad1_.genre as genre3_2_, wad1_.iwad as iwad4_2_, wad1_.name as name5_2_, wad1_.released as released6_2_ from review review0_ inner join wad wad1_ on review0_.wad_id=wad1_.id where review0_.id=?
2022-04-29 10:03:16.780 DEBUG 27716 --- [nPool-worker-19] org.hibernate.SQL                        : select wad1_.id as id1_2_, wad1_.download_link as download2_2_, wad1_.genre as genre3_2_, wad1_.iwad as iwad4_2_, wad1_.name as name5_2_, wad1_.released as released6_2_ from review review0_ inner join wad wad1_ on review0_.wad_id=wad1_.id where review0_.id=?
2022-04-29 10:03:16.780 DEBUG 27716 --- [onPool-worker-5] org.hibernate.SQL                        : select wad1_.id as id1_2_, wad1_.download_link as download2_2_, wad1_.genre as genre3_2_, wad1_.iwad as iwad4_2_, wad1_.name as name5_2_, wad1_.released as released6_2_ from review review0_ inner join wad wad1_ on review0_.wad_id=wad1_.id where review0_.id=?
2022-04-29 10:03:16.780 DEBUG 27716 --- [onPool-worker-9] org.hibernate.SQL                        : select wad1_.id as id1_2_, wad1_.download_link as download2_2_, wad1_.genre as genre3_2_, wad1_.iwad as iwad4_2_, wad1_.name as name5_2_, wad1_.released as released6_2_ from review review0_ inner join wad wad1_ on review0_.wad_id=wad1_.id where review0_.id=?
2022-04-29 10:03:16.791 DEBUG 27716 --- [pool-2-thread-1] org.hibernate.SQL                        : select review0_.id as id1_1_0_, map1_.id as id1_0_1_, review0_.author as author2_1_0_, review0_.description as descript3_1_0_, review0_.map_id as map_id6_1_0_, review0_.published as publishe4_1_0_, review0_.rating as rating5_1_0_, review0_.wad_id as wad_id7_1_0_, map1_.author as author2_0_1_, map1_.enemies as enemies3_0_1_, map1_.name as name4_0_1_, map1_.partime as partime5_0_1_, map1_.wad_id as wad_id6_0_1_ from review review0_ inner join map map1_ on review0_.map_id=map1_.id where review0_.id in (? , ? , ? , ? , ?)
2022-04-29 10:03:16.797  INFO 27716 --- [pool-2-thread-1] c.u.s.service.impl.MapServiceImpl        : Ending query...

Any ideas? Thanks a lot, guys

It seems that I was looking at the wrong field: Wad instead of Map, since the query uses 2 of them. Also, only JOIN FETCH seems to be necessary to eliminate the issue.

Sorry for the mistake!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM