简体   繁体   English

为什么 hibernate @ManyToOne 为同一个表生成多个 select 查询?

[英]Why hibernate @ManyToOne generating multiple select query for same table?

Why Hibernate generate multiple select statement for same table/entity?为什么 Hibernate 为同一个表/实体生成多个 select 语句? Below is @ManyToOne Mapping:以下是@ManyToOne 映射:

class Student{

    @ManyToOne(fetch = FetchType.EAGER)
    @JoinColumn(name = "student_id", insertable = false, updatable = false, referencedColumnName = "student_id")
    Subject subject;
}

Generated HQL:3 times select query is genereating for subject table生成的 HQL:3 次 select 查询正在为主题表生成

Hibernate: 
    select
        subject0_.subject_cd as subject_cd1_12_0_,
        subject0_.subjectname as subject_name_12_0_ 
    from
        smart.vw_subject subject0_
    where
        fundhierar0_.subject_cd =?
Hibernate: 
    select
        subject0_.subject_cd as subject_cd1_12_0_,
        subject0_.subjectname as subject_name_12_0_ 
    from
        smart.vw_subject subject0_
    where
        fundhierar0_.subject_cd =?
Hibernate: 
    select
        subject0_.subject_cd as subject_cd1_12_0_,
        subject0_.subjectname as subject_name_12_0_ 
    from
        smart.vw_subject subject0_
    where
        fundhierar0_.subject_cd =?

Hibernate provides multiple strategies for retrieving data. Hibernate 提供了多种检索数据的策略。 Few are Select , join , batch .很少有Selectjoinbatch

However, this seems like N+1 problem.但是,这似乎是 N+1 问题。 The first query retrieves N records from the database.第一个查询从数据库中检索 N 条记录。 For each Parent, a new query retrieves the Child.对于每个父母,一个新的查询检索孩子。 Therefore for N Parent, N queries retrieve information from the child table.因此,对于 N Parent,N 个查询从子表中检索信息。

The fetching strategy depends on how our application works, so gonna leave you to that.获取策略取决于我们的应用程序是如何工作的,所以就让你去做吧。 You can find more details here您可以在此处找到更多详细信息

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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