简体   繁体   中英

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

Why Hibernate generate multiple select statement for same table/entity? Below is @ManyToOne Mapping:

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

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. Few are Select , join , batch .

However, this seems like N+1 problem. The first query retrieves N records from the database. For each Parent, a new query retrieves the Child. Therefore for N Parent, N queries retrieve information from the child table.

The fetching strategy depends on how our application works, so gonna leave you to that. You can find more details here

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