Hibernate Learning Notes (10)-HQL Query

Source: Internet
Author: User

One, hql a pair of multi-query

Class (1) (multi) Students

/** * Implementation of classes and student internal connection * * SELECT c.*,s.* * FROM classes C INNER JOIN student S * WHERE (C.CID=S.CID) */@Testpublic void Testinnerjoin () {session = Hibernateutils.opensession (); Query query = Session.createquery ("from Classes C" + "INNER join c.students S"); List List = Query.list ();//object[]session.close ();} /** * Urgent Internal connection */@Testpublic void Testinnerjoin_fetch () {session = Hibernateutils.opensession (); Query query = Session.createquery ("from Classes C" + "INNER join fetch c.students S"); List List = Query.list ();//classessession.close ();} /** * LEFT Outer connection * SELECT c.*,s.* * FROM classes C left OUTER JOIN student S * ON (S.CID=C.CID) */@Testpublic void Testleftoutjo In () {session = Hibernateutils.opensession (); Query query = Session.createquery ("from Classes C" + "left outer join c.students S"); List List = Query.list ();//object[]session.close ();} /** * * */@Testpublic void Testleftoutjoin_fetch () {session = Hibernateutils.opensession (); Query query = Session.createquery ("from Classes C"+ "LEFT OUTER join fetch c.students S"); List List = Query.list ();//classessession.close ();} /** * Properties to query from two persisted classes * Note: A query with a constructor cannot exist with a fetch query at the same time * use fetch: I want to take classes * */@Testpublic void Testquerypropertyfromtwo () {session = Hibernateutils.opensession (); list<classstuview> classstuviews = Session.createquery ("Select New Cn.cil.domain.ClassStuView (C.name,s.name)" + "from Classes C inner join c.students S"). List (); Session.close ();

Second, HQL multi-to-many query

Student (many) (multi) courses

/** * */      @Testpublic void Testinnerjoin_fetch () {Session session = Sessionfactory.opensession (); Session.createquery ("From Student s inner join fetch s.courses C"). List (); Session.close ();} /** * Urgent LEFT OUTER connection */@Testpublic void Testleftouterjoin_fetch () {Session session = Sessionfactory.opensession (); Session.createquery ("From Student s left OUTER join fetch s.courses C"). List (); Session.close ();}

Three or one-to-many and many-to-many combinations

Class (1) (multi) Students (multi) courses

/** * Query All classes of all students for all courses */@Testpublic void TestQuery1 () {Session session = Hibernateutils.opensession (); list<classes> Classes = Session.createquery ("from Classes C inner join" + "Fetch c.students s INNER join" + "fetch s . Courses cc "). List (); Session.close ();} @Testpublic void TestQuery2 () {Session session = Hibernateutils.opensession (); list<student> classes = Session.createquery ("From Student s inner join fetch s.classes c INNER JOIN fetch s.courses CC "). List (); Session.close ();}

Practical Scenarios:

The user logs on to the system, determines the permissions that the user has, and displays the menu within the scope of the permission

According to username check the role of users according to the role of check permissions

CreateQuery ("from Privilege p INNER join fetch p.roles R INNER JOIN fetch r.users u where u.username=?")

Hibernate cannot be used when the relationship between tables is too complex, and HQL's writing can be quite cumbersome, using a view to solve the problem, with JDBC SQL

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Hibernate Learning Notes (10)-HQL Query

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.