Connections are divided into internal connections, external connections, and full connections.
External connections include left outer connections and right connections.
Internal connections are also called natural connections.
Select * cannot be written in the brackets of createquery *.
MySQL does not support full join.
Inner join: returns only rows with the same link fields in two tables.
Left join: returns records that include all records in the left table and the link fields in the right table.
Table A left join table B left join is based on table A, all data in Table A, some combinations in Table B, and none are null.
Right join: returns all records in the right table that are equal to the link fields in the left table.
Table A right join table B right join is based on table B, all data in Table B, some combinations in Table A, and none are null.
Select C. *, O. * from customers C, orders owhere C. ID = O. CID full join
Select C. *, O. * from customers C inner joinorders o on C. ID = O. CID equivalent inner join
Select C. *, O. * from customers C left joinorders o on C. ID = O. CID left Outer Join
// Connection Query
// Internal connection
@ Test
Public voidJoin2 (){
Session session = hibernateutil.Getsession();
// Select C. ID, C. realname, O. ID, O. Number fromcustomers as C left join C. Orders as O where c. ID = 1
List <object []> customer =Session. createquery ("select C. ID, C. realname, O. ID, O. number from customersas C left join C. orders as O where c. id = 1 "). list ();
For(Object [] C: customer ){
System.Out. Println (C [0] + "----" + C [1]);
}
}
Result After running:
Hibernate: selectcustomers0 _. ID as col_0_0 _, customers0 _. realname as col_1_0 _, orders1 _. id ascol_2_0 _, orders1 _. number as col_3_0 _ from pros. customers customers0 _ leftouter join pros. orders orders1 _ on customers0 _. id = orders1 _. CID wherecustomers0 _. id = 1
1----435
// Criteria method Query
@ Test
Public voidJoin3 (){
Session session = hibernateutil.Getsession();
/* Criteria = session. createcriteria (customers. Class). Add (restrictions. Like ("realname", "% A %"); // The returned criteria object
List <customers> customers = criteria. List ();*/
List <customers> customers =Session. createcriteria (MERS MERs.Class)
. Add (restrictions.Like("Realname", "% A % "))
. Createcriteria ("orders ")
. Add (restrictions.Like("Number", "% 223% "))
. List (); // Chain Query
For(Customers C: customers ){
System.Out. Println (C. getemail ());
}
}
// Scalar Query
@ Test
Public voidJoin4 (){
Session session = hibernateutil.Getsession();
// Session. createsqlquery ("select * from MERs"). List (); array is returned by default.
List <customers> customers =Session. createsqlquery ("select * from MERs"). addentity (customers.Class). List (); // With. addentity (customers. Class) added, the returned object is not an array but an object.
For(Customers C: customers ){
System.Out. Println (C. getemail ());
}
}