Hibernate hql Multi-table query

Source: Internet
Author: User

HQL Internal Connection Query

For the HQL link query, the query is two tables of data, the data of the two tables is first saved in the array, and then each array is saved in the list collection to return

Code snippet:

1 @Test2     //Internal Connection3      Public voidfun1 () {4Sessionfactory factory =NULL;5Session session =NULL;6Transaction tx =NULL;7         Try {8Factory =tools.getsessionfactory ();9Session =factory.opensession ();Tentx =session.begintransaction (); One             //Mans is a collection property of the Linkman entity class that is associated with the Customer entity class AQuery query = Session.createquery ("from Customer C inner join C.mans"); -List List =query.list (); - Tx.commit ();  the}Catch(Exception e) { - Tx.rollback (); - e.printstacktrace (); -}finally { + session.close (); -         } +}
View Code

Debug

HQL Urgent Internal Connection query

HQL the urgent internal connection query, only need to add a fetch on the inner JOIN query, as follows:

1  from Inner Join Fetch C.mans

The main difference between an urgent inner connection and an inner join is that each part of a list that is connected is an array of objects, and the list that is urgently connected to each part returns an object.

Code snippet:

1 @Test2     //Urgent Internal Connection3      Public voidfun2 () {4Sessionfactory factory =NULL;5Session session =NULL;6Transaction tx =NULL;7         Try {8Factory =tools.getsessionfactory ();9Session =factory.opensession ();Tentx =session.begintransaction (); One             //Mans is a collection property of the Linkman entity class that is associated with the Customer entity class AQuery query = Session.createquery ("from Customer C inner join fetch C.mans"); -List List =query.list (); - Tx.commit ();  the}Catch(Exception e) { - Tx.rollback (); - e.printstacktrace (); -}finally { + session.close (); -         } +}
View Code

Debug:

Left outer connection of HQL

HQL statement for LEFT OUTER join:

1  from  Left outer Join Associates another entity's properties

Code snippet:

1 @Test2     //Left outer connection3      Public voidFun3 () {4Sessionfactory factory =NULL;5Session session =NULL;6Transaction tx =NULL;7         Try {8Factory =tools.getsessionfactory ();9Session =factory.opensession ();Tentx =session.begintransaction (); OneQuery query = Session.createquery ("From Customer C left outer join C.mans"); AList List =query.list (); - Tx.commit ();  -}Catch(Exception e) { the Tx.rollback (); - e.printstacktrace (); -}finally { - session.close (); +         } -}
View Code

Debug:

HQL of the left outside of the urgent connection

Urgent LEFT OUTER JOIN HQL statement:

 from  Left outer Join The property of another entity class in this class that is associated with a fetch

Code snippet:

1 @Test2     //urgent LEFT outer connection3      Public voidFun4 () {4Sessionfactory factory =NULL;5Session session =NULL;6Transaction tx =NULL;7         Try {8Factory =tools.getsessionfactory ();9Session =factory.opensession ();Tentx =session.begintransaction (); OneQuery query = Session.createquery ("From Customer C left OUTER join fetch C.mans"); AList List =query.list (); - Tx.commit ();  -}Catch(Exception e) { the Tx.rollback (); - e.printstacktrace (); -}finally { - session.close (); +         } -}
View Code

Debug:

Hibernate hql Multi-table 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.