尽管使用了自定义查询,但Hibernate仍在查询N + 1

时间:2019-05-20 14:21:40

标签: java sql hibernate spring-boot jpa

我在两个实体Review和Reviewer之间创建了双向的OneToOne关系。审阅者是拥有者,而审阅者是反向者。我注意到,当我调用findByBedrijfId()方法时(当时没有自定义@Query),Hibernate查询了n +1。我希望通过使用LEFT OUTER JOIN编写自定义@Query来解决这一问题(请参见ReviewRepository)。但是相反,Hibernate仍然查询n + 1,我已经阅读了有关通过OneToOne关系设置延迟为false的延迟获取的信息,如这篇文章:https://stackoverflow.com/a/47768154/3262578,但没有成功。谁知道我该如何防止这种行为。

2019-05-20 16:15:35.741 DEBUG 97403 --- [nio-8089-exec-1] org.hibernate.SQL                        : SELECT * FROM bedrijfreviews br LEFT OUTER JOIN reviewerinfo ri ON br.reviewid = ri.reviewid WHERE br.bedrijfId = ?
2019-05-20 16:15:35.770 DEBUG 97403 --- [nio-8089-exec-1] org.hibernate.SQL                        : select reviewer0_.reviewid as reviewid1_4_0_, reviewer0_.email as email2_4_0_, reviewer0_.leeftijdscategorie as leeftijd3_4_0_, reviewer0_.naam as naam4_4_0_, review1_.reviewid as reviewid1_0_1_, review1_.administratie as administ2_0_1_, review1_.bedrijfid as bedrijfi3_0_1_, review1_.commentaar as commenta4_0_1_, review1_.datum as datum5_0_1_, review1_.gemiddelde_cijfer as gemiddel6_0_1_, review1_.minpunten as minpunte7_0_1_, review1_.nakomen_afspraken as nakomen_8_0_1_, review1_.persoonlijke_benadering as persoonl9_0_1_, review1_.pluspunten as pluspun10_0_1_, review1_.reactiesnelheid as reactie11_0_1_, review1_.vakkennis as vakkenn12_0_1_ from reviewerinfo reviewer0_ left outer join bedrijfreviews review1_ on reviewer0_.reviewid=review1_.reviewid where reviewer0_.reviewid=?
2019-05-20 16:15:35.775 DEBUG 97403 --- [nio-8089-exec-1] org.hibernate.SQL                        : select reviewer0_.reviewid as reviewid1_4_0_, reviewer0_.email as email2_4_0_, reviewer0_.leeftijdscategorie as leeftijd3_4_0_, reviewer0_.naam as naam4_4_0_, review1_.reviewid as reviewid1_0_1_, review1_.administratie as administ2_0_1_, review1_.bedrijfid as bedrijfi3_0_1_, review1_.commentaar as commenta4_0_1_, review1_.datum as datum5_0_1_, review1_.gemiddelde_cijfer as gemiddel6_0_1_, review1_.minpunten as minpunte7_0_1_, review1_.nakomen_afspraken as nakomen_8_0_1_, review1_.persoonlijke_benadering as persoonl9_0_1_, review1_.pluspunten as pluspun10_0_1_, review1_.reactiesnelheid as reactie11_0_1_, review1_.vakkennis as vakkenn12_0_1_ from reviewerinfo reviewer0_ left outer join bedrijfreviews review1_ on reviewer0_.reviewid=review1_.reviewid where reviewer0_.reviewid=?
2019-05-20 16:15:35.777 DEBUG 97403 --- [nio-8089-exec-1] org.hibernate.SQL                        : select reviewer0_.reviewid as reviewid1_4_0_, reviewer0_.email as email2_4_0_, reviewer0_.leeftijdscategorie as leeftijd3_4_0_, reviewer0_.naam as naam4_4_0_, review1_.reviewid as reviewid1_0_1_, review1_.administratie as administ2_0_1_, review1_.bedrijfid as bedrijfi3_0_1_, review1_.commentaar as commenta4_0_1_, review1_.datum as datum5_0_1_, review1_.gemiddelde_cijfer as gemiddel6_0_1_, review1_.minpunten as minpunte7_0_1_, review1_.nakomen_afspraken as nakomen_8_0_1_, review1_.persoonlijke_benadering as persoonl9_0_1_, review1_.pluspunten as pluspun10_0_1_, review1_.reactiesnelheid as reactie11_0_1_, review1_.vakkennis as vakkenn12_0_1_ from reviewerinfo reviewer0_ left outer join bedrijfreviews review1_ on reviewer0_.reviewid=review1_.reviewid where reviewer0_.reviewid=?

public interface ReviewRepository extends JpaRepository<Review, Long> {

    @Query(value = "SELECT * FROM bedrijfreviews br LEFT OUTER JOIN reviewerinfo ri ON br.reviewid = ri.reviewid WHERE br.bedrijfId = :id", nativeQuery = true)
    List<Review> findByBedrijfId(@Param("id") Long id);
}
@Entity
@Data
public class Review {

    @JsonProperty("bedrijfId")
    @Column(name = "bedrijfid")
    private Long bedrijfId;

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @JsonIgnore
    private Long reviewId;

    @OneToOne(mappedBy = "review", cascade=CascadeType.ALL)
    @JoinColumn(name = "reviewid", unique = true)
    private Reviewer reviewer;
}
@Entity
@Data
public class Reviewer {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "reviewid")
    @JsonIgnore
    private Long reviewId;

    @OneToOne(cascade=CascadeType.ALL)
    @JoinColumn(name = "reviewid", unique = true)
    @JsonIgnore
    private Review review;
}

致谢

巴特

0 个答案:

没有答案