Hibernate join fetch

Source: Internet
Author: User

Join fetch is actually equivalent to the connection part in the database. The default value is inner join. Left outer join is also supported. Join fetch is mainly used in HQL. It is mainly reflected in one-to-many and many-to-many Relational Tables, such as class Student {private int id; private int name; private Team team; get {...}; Set {...};} Class Team {private int tid; private String name; private Set <Student> students; get {...}; Set {...} ';} By default, fetch = lazy is used to query all Student in the Team. ---------------- query --------------------- Team t = (Team) session. load (Team. class, 3); Set <Student> sets = t. getStudents (); for (Student s: sets) {System. out. println (s. getId () + "" + s. getName ();} ---------------- query result ----------------- Hibernate: select team0 _. cid as cid0_0 _, team0 _. name as name0_0 _ from team0 _ where team0 _. cid =? Hibernate: select students0 _. cid as cid0_1 _, students0 _. id as id1 _, students0 _. id as id1_0 _, students0 _. cid as cid1_0 _, students0 _. name as name1_0 _ from Student students0 _ where students0 _. cid =? --------------------------------------- The query is performed twice. The first query is performed on the specified Team, and the second query is performed on all Student under the specified Team. Join fetch is used to reduce the number of queries. ---------------- Query --------------------- List <Class> lists = session. createQuery ("from Team t join fetch c. students order by t. cid "). list (); for (Team t: lists) {System. out. println (t. getCid () + "" + t. getName () + ""); for (Student s: t. getStudents () {System. out. println ("" + s. getId () + "" + s. getName () + "" + s. getTeam (). getCid () ;}------------------ query result ----------------- Hibernate: select team0 _. cid as cid0_0 _, students1 _. id as id1_1 _, team0 _. name as name0_0 _, students1 _. cid as cid1_1 _, students1 _. name as name1_1 _, students1 _. cid as cid0_0 __, students1 _. id as id0 _ from team class0 _ inner join Student students1 _ on team0 _. cid = students1 _. cid order by team0 _. cid -----------------------------------------

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.