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