- Hql:hibernate Query Language.
- Characteristics:
- >> 1, similar to SQL, the syntax in SQL is basically straightforward to use.
- >> 2,sql queries are columns in tables and tables, and HQL queries are objects and properties in objects.
- >> 3,hql keywords are case-insensitive, and the class name and property name are case-sensitive.
- >> 4,select can be omitted.
- 1, simple query, employee is entity name instead of table name in database (Object oriented property)
- HQL = "from Employee";
- HQL = "from Employee as E"; //Use aliases
- HQL = "from Employee e"; //Use aliases, as keywords can be omitted
- 2, with filter conditions (aliases can be used): Where
- HQL = "from Employee WHERE id<10";
- HQL = "from Employee e WHERE e.id<10";
- HQL = "from Employee e WHERE e.id<10 and e.id>5";
- 3, with sorting criteria: order BY
- HQL = "from the Employee e WHERE e.id<10 ORDER by E.name";
- HQL = "from Employee e WHERE e.id<10 ORDER by E.name DESC";
- HQL = "from Employee e WHERE e.id<10 ORDER by e.name DESC, id ASC";
- 4, specify the SELECT clause (You cannot use SELECT *)
- HQL = "Select e from Employee e"; //equivalent to "from Employee E"
- HQL = "Select e.name from Employee e"; //Query only one column, the element type of the returned collection is the type of this property
- HQL = "Select E.id,e.name from Employee e"; //Query multiple columns, the element type of the returned collection is an object array
- 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
- 5, execute query, get results (list, uniqueresult, paging)
- Query query = session.createquery ("from Employee e WHERE id<3");
- Query.setfirstresult (0);
- Query.setmaxresults (10); //equal to limit 0,10
- List of two query results, Uniqueresult
- List List = Query.list (); The result of the query is a list collection
- Employee employee = (employee) query.uniqueresult ();//The result of a query is the only result, and when there are multiple results, it throws an exception
- 6, Method chain
- List List = Session.createquery (//
- "from Employee E") //
- . Setfirstresult (0)//
- . Setmaxresults (Ten)//
- . List ();
- 7, aggregate function: Count (), Max (), Min (), AVG (), SUM ()
- HQL = "Select COUNT (*) from Employee"; //The result returned is a long type
- HQL = "select min (id) from Employee"; //The result returned is the type of the id attribute
- 8, group: GROUP By ... Having
- HQL = "Select E.name,count (e.id) from the Employee e GROUP by E.name";
- HQL = "Select E.name,count (e.id) from the Employee e GROUP by E.name have COUNT (e.id) >1";
- HQL = "Select E.name,count (e.id) from the Employee e WHERE id<9 GROUP by E.name have COUNT (e.id) >1";
- HQL = "Select E.name,count (e.id)" + //
- "from Employee E" + //
- "WHERE id<9" + //
- "GROUP by e.name" + //
- "having count (e.id) >1" + //
- "ORDER by Count (e.id) ASC";
- HQL = "Select E.name,count (e.id) as C" + //
- "from Employee E" + //
- "WHERE id<9" + //
- "GROUP by e.name" + //
- "having count (e.id) >1" + //cannot use column aliases in a HAVING clause
- "ORDER by C ASC"; //You can use column aliases in the BY clause
- 9, Connection query/HQL is an object-oriented query
- //>> Inner Connection (inner keyword can be omitted)
- HQL = "Select E.id,e.name,d.name from Employee e JOIN e.department D";
- HQL = "Select E.id,e.name,d.name from Employee e INNER JOIN e.department d";
- //>> LEFT OUTER join (outer keyword can be omitted)
- HQL = "Select E.id,e.name,d.name from Employee e left OUTER JOIN e.department D";
- //>> right outer join (outer keyword can be omitted)
- HQL = "Select E.id,e.name,d.name from Employee e right JOIN e.department d";
- //Can be used in a more convenient way
- HQL = "Select E.id,e.name,e.department.name from Employee e";
- 10, using Parameters when querying
- >> method One: use '? ' Occupy position
- HQL = "from Employee e WHERE id between?" and? ";
- List List2 = session.createquery (HQL)//
- . Setparameter (0, 5)//Set parameter, index of 1th parameter is 0.
- . Setparameter (1, up )//
- . List ();
- >> method Two: Use variable name
- HQL = "from Employee e WHERE id between:idmin and:idmax";
- List list3 = session.createquery (HQL)//
- . Setparameter ("Idmax", + )//
- . Setparameter ("Idmin", 5)//
- . List ();
- When the parameter is a collection, be sure to use Setparameterlist () to set the parameter value
- HQL = "from the Employee e WHERE ID in (: IDs)";
- List list4 = session.createquery (HQL)//
- . Setparameterlist ("IDs", new object[] { 1, 2, 3, 5, 8, + })// /c4>
- . List ();
- 11,update with Delete, does not notify session cache
- >> Update
- int result = Session.createquery (//
- "UPDATE Employee e SET e.name=?" WHERE id>15 ")//
- . Setparameter (0, "anonymous")//
- . executeupdate (); //Returns the result of the int type, indicating how many rows were affected.
- >> Delete
- int result1 = Session.createquery (//
- "DELETE from Employee e WHERE id>15") //
- . executeupdate (); //Returns the result of the int type, indicating how many rows were affected.
Summary of HQL statements commonly used in hibernate