Summary of HQL statements commonly used in hibernate

Source: Internet
Author: User
Tags aliases

  1. Hql:hibernate Query Language.
  2. Characteristics:
  3. >> 1, similar to SQL, the syntax in SQL is basically straightforward to use.
  4. >> 2,sql queries are columns in tables and tables, and HQL queries are objects and properties in objects.
  5. >> 3,hql keywords are case-insensitive, and the class name and property name are case-sensitive.
  6. >> 4,select can be omitted.
  7. 1, simple query, employee is entity name instead of table name in database (Object oriented property)
  8. HQL = "from Employee";
  9. HQL = "from Employee as E"; //Use aliases
  10. HQL = "from Employee e"; //Use aliases, as keywords can be omitted
  11. 2, with filter conditions (aliases can be used): Where
  12. HQL = "from Employee WHERE id<10";
  13. HQL = "from Employee e WHERE e.id<10";
  14. HQL = "from Employee e WHERE e.id<10 and e.id>5";
  15. 3, with sorting criteria: order BY
  16. HQL = "from the Employee e WHERE e.id<10 ORDER by E.name";
  17. HQL = "from Employee e WHERE e.id<10 ORDER by E.name DESC";
  18. HQL = "from Employee e WHERE e.id<10 ORDER by e.name DESC, id ASC";
  19. 4, specify the SELECT clause (You cannot use SELECT *)
  20. HQL = "Select e from Employee e"; //equivalent to "from Employee E"
  21. HQL = "Select e.name from Employee e"; //Query only one column, the element type of the returned collection is the type of this property
  22. HQL = "Select E.id,e.name from Employee e"; //Query multiple columns, the element type of the returned collection is an object array
  23. HQL = "Select New Employee (e.id,e.name) from employee E"; //You can use the new syntax to specify that some of the properties of the query are encapsulated in the object
  24. 5, execute query, get results (list, uniqueresult, paging)
  25. Query query = session.createquery ("from Employee e WHERE id<3");
  26. Query.setfirstresult (0);
  27. Query.setmaxresults (10); //equal to limit 0,10
  28. List of two query results, Uniqueresult
  29. List List = Query.list (); The result of the query is a list collection
  30. Employee employee = (employee) query.uniqueresult ();//The result of a query is the only result, and when there are multiple results, it throws an exception
  31. 6, Method chain
  32. List List = Session.createquery (//
  33. "from Employee E") //  
  34. . Setfirstresult (0)//
  35. . Setmaxresults (Ten)//
  36. . List ();
  37. 7, aggregate function: Count (), Max (), Min (), AVG (), SUM ()
  38. HQL = "Select COUNT (*) from Employee"; //The result returned is a long type
  39. HQL = "select min (id) from Employee"; //The result returned is the type of the id attribute
  40. 8, group: GROUP By ... Having
  41. HQL = "Select E.name,count (e.id) from the Employee e GROUP by E.name";
  42. HQL = "Select E.name,count (e.id) from the Employee e GROUP by E.name have COUNT (e.id) >1";
  43. HQL = "Select E.name,count (e.id) from the Employee e WHERE id<9 GROUP by E.name have COUNT (e.id) >1";
  44. HQL = "Select E.name,count (e.id)" + //
  45. "from Employee E" + //
  46. "WHERE id<9" + //
  47. "GROUP by e.name" + //
  48. "having count (e.id) >1" + //
  49. "ORDER by Count (e.id) ASC";
  50. HQL = "Select E.name,count (e.id) as C" + //
  51. "from Employee E" + //
  52. "WHERE id<9" + //
  53. "GROUP by e.name" + //
  54. "having count (e.id) >1" + //cannot use column aliases in a HAVING clause
  55. "ORDER by C ASC"; //You can use column aliases in the BY clause
  56. 9, Connection query/HQL is an object-oriented query
  57. //>> Inner Connection (inner keyword can be omitted)
  58. HQL = "Select E.id,e.name,d.name from Employee e JOIN e.department D";
  59. HQL = "Select E.id,e.name,d.name from Employee e INNER JOIN e.department d";
  60. //>> LEFT OUTER join (outer keyword can be omitted)
  61. HQL = "Select E.id,e.name,d.name from Employee e left OUTER JOIN e.department D";
  62. //>> right outer join (outer keyword can be omitted)
  63. HQL = "Select E.id,e.name,d.name from Employee e right JOIN e.department d";
  64. //Can be used in a more convenient way
  65. HQL = "Select E.id,e.name,e.department.name from Employee e";
  66. 10, using Parameters when querying
  67. >> method One: use '? ' Occupy position
  68. HQL = "from Employee e WHERE id between?" and? ";
  69. List List2 = session.createquery (HQL)//
  70. . Setparameter (0, 5)//Set parameter, index of 1th parameter is 0.
  71. . Setparameter (1, up )//
  72. . List ();
  73. >> method Two: Use variable name
  74. HQL = "from Employee e WHERE id between:idmin and:idmax";
  75. List list3 = session.createquery (HQL)//
  76. . Setparameter ("Idmax", + )//
  77. . Setparameter ("Idmin", 5)//
  78. . List ();
  79. When the parameter is a collection, be sure to use Setparameterlist () to set the parameter value
  80. HQL = "from the Employee e WHERE ID in (: IDs)";
  81. List list4 = session.createquery (HQL)//
  82. . Setparameterlist ("IDs", new object[] { 1, 2, 3, 5, 8, + })// /c4>
  83. . List ();
  84. 11,update with Delete, does not notify session cache
  85. >> Update
  86. int result = Session.createquery (//
  87. "UPDATE Employee e SET e.name=?" WHERE id>15 ")//
  88. . Setparameter (0, "anonymous")//
  89. . executeupdate ();  //Returns the result of the int type, indicating how many rows were affected.
  90. >> Delete
  91. int result1 = Session.createquery (//
  92. "DELETE from Employee e WHERE id>15") //  
  93. . executeupdate (); //Returns the result of the int type, indicating how many rows were affected.

Summary of HQL statements commonly used in hibernate

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.