Hibernate (5) _ hql statement and hibernate _ hql statement
Hql (hibernate query language) is powerful.
First, let's recall the previous operations on data objects:
① Delete session. delete (object)
② Save session. save (object) session. persist (object)
③ Modify obj. setXXX ();
④ Query session. load (***. class, 1); session. get (***. class, 1 );
However, we can only perform simple data operations. To perform complex operations, we need to use hql statements.
Hql details
The hibernate designer recommends that when designing a table, each table should have a primary key andIt is recommended that the primary key not include the business logic(Do not make sense)
For example:Product table
Id productNo name price
1 bi001 refrigerator 1000
2 nj111 computer 2000
It is recommended that you do not design the product number as a primary key. If the product number changes, the entire data table will change. However, if you design another primary key id that does not contain business information
The price data for variable numbers and names is stable.
We use the hibernate tool to automatically generate domain objects and ing files. If our table has a primary foreign key relationship, we should first map the primary table and then map it from the table.
Three tables are created: the courses selected by students and students.
The relationship between the table and the ing file is as follows (we will learn the one-to-many, many-to-one, and many-to-many mappings later ):
* Retrieve all data objects in a table:
List <Student> list = session. createQuery ("from Student"). list;
* UniqueResult Method
If we retrieve an object and know that there is only one object at most, we recommend that you use this method to improve efficiency:
The usage is as follows:
Student s = (Student) session. createQuery ("from Student where sid = '000000'"). uniqueResult ();
System. out. println (s. getSname ());
* Distinct usage
Filter duplicate records
// For example, show the gender and age of all students.
List list = session. createQuery ("select distinct sage, ssex from Student"). list ();
For (int I = 0; I <list. size (); I ++ ){
Object [] objs = (Object []) list. get (I );
System. out. println (objs [0]. toString () + "" + objs [1]. toString ());
}
* Between and ..
List list = session. createQuery ("select distinct sage, ssex, sname from Student where sage between 20 and 22"). list ();
For (int I = 0; I <list. size (); I ++ ){
Object [] objs = (Object []) list. get (I );
System. out. println (objs [0]. toString () + "" + objs [1]. toString () + objs [2]. toString ());
}
* In/not in
// Query student information of computer and foreign languages
List <Student> list = session. createQuery ("from Student where sdept in ('computer system ', 'foreign languages')"). list ();
// Retrieve for enhancement
For (Student s: list ){
System. out. println (s. getSname () + "" + s. getSaddress () + "" + s. getSdept ());
}
* Use group
// Display the average age of students in each department
List <Object []> list = session. createQuery ("select avg (sage), sdept from Student group by sdept"). list ();
// Retrieve for enhancement
For (Object [] obj: list ){
System. out. println (obj [0]. toString () + "" + obj [1]. toString ());
}
*Having
1. filter the group query results. For example, show the department names with more than 3 students.
List <Object []> list = session. createQuery ("select count (*) as c1, sdept from Student group by sdept having count (*)> 3 "). list ();
// Retrieve for enhancement
For (Object [] obj: list ){
System. out. println (obj [0]. toString () + "" + obj [1]. toString ());
}
// Query the number of girls in each department
List <Object []> list = session.
CreateQuery ("select count (*) as c1, sdept from Student where ssex = 'F' group by sdept"). list ();
// Retrieve for enhancement
For (Object [] obj: list ){
System. out. println (obj [0]. toString () + "" + obj [1]. toString ());
}
2. How many people are there in the computer system? -> If we return a column of data
// In this case, we can retrieve the list-> object instead of list-> Object [] directly.
List <Object []> list = session.
CreateQuery ("select sage from Student where sdept = 'computer system'"). list ();
// Retrieve for enhancement
For (Object obj: list ){
System. out. println (obj. toString ());
}
3. query the highest score and lowest score of the course No. 11.
List <Object []> list = session.
CreateQuery ("select 11, max (grade), min (grade) from Studcourse where course. cid = 11"). list ();
// Retrieve for enhancement
For (Object [] obj: list ){
System. out. println (obj [0]. toString () + "max =" + obj [1]. toString () + "min =" + obj [2]. toString ());
}
4.Calculate the number of failed students in each subject.
List <Object []> list = session.
CreateQuery ("select count (*), student. sdept from Studcourse where grade <60 group by student. sdept"). list ();
// Retrieve 1. for Enhancement
For (Object [] obj: list ){
System. out. println (obj [0]. toString () + "" + obj [1]. toString ());
}
Parameter binding case (jdbc-> PreparedStatement setXXX)
Three Advantages of parameter binding are as follows:
Interview Questions:If parameter binding is not used, how does one prevent SQL injection during logon?
Name password
Train of Thought: query the password corresponding to the user name in the database through the user name, and then compare it with the secret entered by the user. If it is equal, the user and the method; otherwise, it is invalid.
There are two types of parameter binding:
Query q = session. createQuery (from Student where sdept =: dept and sage>: age)
If our parameter is:ColonGiven in the form, our parameter binding should be as follows:
List <Student> list = session. createQuery ("from Student where sdept =: a1 and sage>: sage ")
. SetString ("a1", "computer system"). setString ("sage", "2"). list ();
There is also a form:
Query q = session. createQuery (from Student where sdept =? And sage> ?)
If our parameter is?FormGiven, the parameter binding should be:
List <Student> list = session. createQuery ("from Student where sdept =? And sage>? ")
. SetString (0, "computer system"). setString (1, "2"). list ();
Parameter binding, which can be written separately: the form is as follows:
Query query = session. createQuery ("from Student where sdept =? And sage>? ");
Query. setString (0, "computer system ");
Query. setString (1, "2 ");
List <Student> list = query. list ();
For (int I = 0; I <list. size (); I ++ ){
Student s = list. get (I );
System. out. println (s. getSname () + "" + s. getSage ());
}