fetch设置为lazy,查询Parent对象的时候,默认只有Parent的内容,并不包含child的信息,这样可以避免n+1问题。
但又想查询parent时把children得到,就可以用fetch。
fetch主动将child得到并set到children列表中。
HQL语句:from Parent p left join fetch p.children
本文介绍了在查询Parent对象时如何通过设置fetch为lazy来避免n+1查询问题,并使用fetch主动加载children列表的方法。通过HQL示例展示了如何在查询Parent对象的同时获取其关联的children信息。
fetch设置为lazy,查询Parent对象的时候,默认只有Parent的内容,并不包含child的信息,这样可以避免n+1问题。
但又想查询parent时把children得到,就可以用fetch。
fetch主动将child得到并set到children列表中。
HQL语句:from Parent p left join fetch p.children

被折叠的 条评论
为什么被折叠?