Hibernate-hql Summary

Source: Internet
Author: User
Tags new set

1. query all fields of the entire ing object

Java code

  1. // The direct from query shows a ing object, that is, querying all fields of the entire ing object.
  2. String hql = "from users ";
  3. Query query = session. createquery (hql );
  4. List Users = query. List ();
  5. For (users user: Users ){
  6. System. Out. println (user. getname () + ":" + User. getpasswd () + ":" + User. GETID ());
  7. }
  8. Output result:
  9. Name1: password1: 1
  10. Name2: password2: 2
  11. Name3: password3: 3
// Directly from is a ing object, that is, query all the fields of the entire ing object string hql = "from users"; query = session. createquery (hql); List
 
  
Users = query. list (); For (users user: Users) {system. out. println (user. getname () + ":" + User. getpasswd () + ":" + User. GETID ();} the output result is: name1: password1: 1 name2: password2: 2 name3: password3: 3
 

2. query a single Field

Java code

  1. // Query a single Field
  2. String hql = "Select name from users ";
  3. Query query = session. createquery (hql );
  4. List List = query. List ();
  5. For (string STR: List ){
  6. System. Out. println (STR );
  7. }
  8. Output result:
  9. Name1
  10. Name2
  11. Name3
// Query a single field string hql = "Select name from users"; query = session. createquery (hql); List
 
  
List = query. List (); For (string STR: List) {system. Out. println (STR);} the output result is name1 name2 name3.
 

3. query several fields

Java code

  1. // Query several fields
  2. String hql = "Select name, passwd from users ";
  3. Query query = session. createquery (hql );
  4. // By default, the queried list stores an object array.
  5. ListList = query. List ();
  6. For (object [] object: List ){
  7. String name = (string) object [0];
  8. String passwd = (string) object [1];
  9. System. Out. println (name + ":" + passwd );
  10. }
  11. Output result:
  12. Name1: password1
  13. Name2: password2
  14. Name3: password3
// Query several of the fields string hql = "Select name, passwd from users"; query = session. createquery (hql); // The list queried by default stores an object array list.List = query. list (); For (object [] object: List) {string name = (string) object [0]; string passwd = (string) object [1]; system. out. println (name + ":" + passwd);} output: name1: password1 name2: password2 name3: password3

4. Modifying the default query result (query. List () is not returned in the form of an object [] array, but in the form of a list

Reference

// Query several fields and add new list (). Note that l in list is in lower case. You do not need to import the package. In this way, the default object array is no longer stored in the list through query. List (), but the list set.
String hql = "select new list (name, passwd) from users ";
Query query = session. createquery (hql );
// By default, the queried list stores an object array, but the list is not the default object array, but a list set.
List List = query. List ();
For (list User: List ){
String name = (string) user. Get (0 );
String passwd = (string) user. Get (1 );
System. Out. println (name + ":" + passwd );
}
/**
Output result:
Name1: password1
Name2: password2
Name3: password3
*/

5. Modifying the default query result (query. List () is not returned in the form of an object [] array, but in the form of a map.

Java code

  1. // Query several fields and add new map (). Note that m in map is in lower case. You do not need to import the package. In this way, the default object array is no longer stored in the list through query. List (), but the map set.
  2. String hql = "select new map (name, passwd) from users ";
  3. Query query = session. createquery (hql );
  4. // The list queried by default stores an object array, but the list stores the default object array instead of the map array.
  5. List
    List = query. List ();
  6. For (map user: List ){
  7. // All the field values in a record are an element in map. The key is a string of 0, 1, 2, 3..., and the value is the field value.
  8. // If you change hql to string hql = "select new map (name as username, passwd as password) from users";, the key is not a string 0, 1, 2... but "username" and "password ".
  9. String name = (string) user. get ("0"); // get ("0"); is get (key), note: 0, 1, 2... is a string, not an integer
  10. String passwd = (string) user. Get ("1 ");
  11. System. Out. println (name + ":" + passwd );
  12. }
  13. /**
  14. Output result:
  15. Name1: password1
  16. Name2: password2
  17. Name3: password3
  18. */
// Query several fields and add new map (). Note that m in map is in lower case. You do not need to import the package. in the list (), the default object array is no longer stored, but Map sets string hql = "select new map (name, passwd) from users "; query query = session. createquery (hql); // The list queried by default stores an object array, but the list stores a list instead of the default object array.List = query. list (); For (map user: List) {// all the field values in a record are an element in the map. The key is a string 0, 1, 2, 3 ...., value is the field value // if you change hql to: String hql = "select new map (name as username, passwd as password) from users";, the key is not the string 0, 1, 2... but "username", "password", string name = (string) user. get ("0"); // get ("0"); is get (key), note: 0, 1, 2... is a string, not an integer string passwd = (string) user. get ("1"); system. out. println (name + ":" + passwd);}/** the output result is: name1: password1 name2: password2 name3: password3 */

6. modify the default query result (query. list () is returned in the form of a set instead of an object [] array. However, because there are no repeated elements in the Set, the values of username and password cannot be the same. Change hql to string hql = "select new set (name, passwd) from users ";

7. Modify the default query result (query. List (). The result is not returned in the form of an object [] array and is returned in a custom type.

Custom class:

Java code

  1. Package com. domain;
  2. Public class myuser {
  3. Private string username;
  4. Private string password;
  5. // Because: String hql = "select new COM. domain. myuser (name, passwd) from users"; therefore, you must have a constructor that accepts two parameters.
  6. Public myuser (string username, string password ){
  7. This. Username = username;
  8. This. Password = password;
  9. }
  10. Public String GetUserName (){
  11. Return username;
  12. }
  13. Public void setusername (string username ){
  14. This. Username = username;
  15. }
  16. Public String GetPassword (){
  17. Return password;
  18. }
  19. Public void setpassword (string password ){
  20. This. Password = password;
  21. }
  22. }
Package COM. domain; public class myuser {private string username; private string password; // because: String hql = "select new COM. domain. myuser (name, passwd) from users "; therefore, you must have a constructor that accepts two parameters: Public myuser (string username, string password) {This. username = username; this. password = password;} Public String GetUserName () {return username;} public void setusername (string username) {This. username = username;} Public String GetPassword () {return password;} public void setpassword (string password) {This. password = password ;}}

Java code

  1. // Query. in the list (), the default object array is no longer stored, but the custom class myuser. The package name must be added, string hql = "from users "; the users class in must also be added with the package name, but because of the users. HBM. XML The default value of auto-import is true (so the auto-import attribute can be left empty ).
  2. String hql = "select new COM. domain. myuser (name, passwd) from users ";
  3. Query query = session. createquery (hql );
  4. // By default, the queried list stores an object array, but the list stores no longer the default object array, but the myuser object.
  5. List Myusers = query. List ();
  6. For (myuser: myusers ){
  7. String name = myuser. GetUserName ();
  8. String passwd = myuser. GetPassword ();
  9. System. Out. println (name + ":" + passwd );
  10. }
  11. /**
  12. Output result:
  13. Name1: password1
  14. Name2: password2
  15. Name3: password3
  16. */
// Query. in the list (), the default object array is no longer stored, but the custom class myuser. The package name must be added, string hql = "from users "; the users class in must also be added with the package name, but because of the users. HBM. XML
 
  
The default value of auto-import is true (so the auto-import attribute can be left empty). String hql = "select new COM. domain. myuser (name, passwd) from users "; query = session. createquery (hql); // The list queried by default stores an object array, but the list stores a list of myuser objects instead of the default object array.
  
   
Myusers = query. list (); For (myuser: myusers) {string name = myuser. getUserName (); string passwd = myuser. getPassword (); system. out. println (name + ":" + passwd);}/** the output result is: name1: password1 name2: password2 name3: password3 */
  
 

8: Conditional Query

Java code

  1. // Condition query. The index value starts from 0 and the index location. Set parameters through setstring and setparameter
  2. String hql = "from users where name =? And passwd =? ";
  3. Query query = session. createquery (hql );
  4. // There are 1st Methods
  5. // Query. setstring (0, "name1 ");
  6. // Query. setstring (1, "password1 ");
  7. // There are 2nd Methods
  8. Query. setparameter (0, "name1", hibernate. String );
  9. Query. setparameter (1, "password1", hibernate. String );
  10. List List = query. List ();
  11. For (users: List ){
  12. System. Out. println (users. GETID ());
  13. }
// Condition query. The index value starts from 0 and the index location. Use setstring and setparameter to set the string hql = "from users where name =? And passwd =? "; Query = session. createquery (hql); // 1st Methods // query. setstring (0, "name1"); // query. setstring (1, "password1"); // query in 2nd modes. setparameter (0, "name1", hibernate. string); query. setparameter (1, "password1", hibernate. string); List
 
  
List = query. List (); For (users: List) {system. Out. println (users. GETID ());}
 

Java code

  1. // Condition query, custom index name (parameter name): username,: password. set parameters through setstring and setparameter
  2. String hql = "from users where name =: username and passwd =: Password ";
  3. Query query = session. createquery (hql );
  4. // There are 1st Methods
  5. // Query. setstring ("username", "name1 ");
  6. // Query. setstring ("password", "password1 ");
  7. // 2nd methods, 3rd parameters determine the type
  8. Query. setparameter ("username", "name1", hibernate. String );
  9. Query. setparameter ("password", "password1", hibernate. String );
  10. List List = query. List ();
  11. For (users: List ){
  12. System. Out. println (users. GETID ());
  13. }
// Condition query, custom index name (parameter name): username,: password. set the parameter string hql = "from users where name =: username and passwd =: Password" Through setstring and setparameter; query = session. createquery (hql); // 1st Methods // query. setstring ("username", "name1"); // query. setstring ("password", "password1"); // 2nd methods, 3rd parameters determine the type of query. setparameter ("username", "name1", hibernate. string); query. setparameter ("password", "password1", hibernate. string); List
 
  
List = query. List (); For (users: List) {system. Out. println (users. GETID ());}
 

Java code

  1. // For conditional query, set parameters through setproperties
  2. String hql = "from users where name =: username and passwd =: Password ";
  3. Query query = session. createquery (hql );
  4. // The two attributes of the myuser class must correspond to: username and: password.
  5. Myuser = new myuser ("name1", "password1 ");
  6. Query. setproperties (myuser );
  7. List List = query. List ();
  8. For (users: List ){
  9. System. Out. println (users. GETID ());
  10. }

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.