Hibernate fetching strategy - when to use “join” and when to use “select”?

前端 未结 6 2126
栀梦
栀梦 2020-12-22 18:54

Most Hibernate associations support \"fetch\" parameter:

fetch=\"join|select\"

with \"select\" being default value.

How to decide

6条回答
  •  天命终不由人
    2020-12-22 19:45

    Select will fetch child items by issuing a new query to the database for them. Join will fetch child items by joining them into the parent's query. So that's why you're seeing similar performance, even with a drop in number of queries.

    Select:

    SELECT * FROM parent WHERE id=(whatever)
    SELECT * FROM child WHERE id=(parent.child.id)
    

    Join:

    SELECT *
    FROM parent
    LEFT OUTER JOIN child ON parent.child.id=child.id
    WHERE parent.id=(whatever)
    

    As to when to use one over the other... Not entire sure. It likely depends on the database system. If one was always better than the other, I doubt they would bother to give you the option! If you're seeing similar performance for each, I wouldn't worry about it.

提交回复
热议问题