Select clauseUsed to select a specified attribute or directly select an object. Of course, the select attribute must be an attribute contained in the from persistence class. For example:
select p.name from Person as p
Select can select any attribute, that is, not only the direct attribute of the persistence class can be selected, but also the attribute contained in the component property, such:
select p.name.firstname from Person as p
NormallyResult is setThe Set element is an array composed of instances and attributes after select. In special cases, if only one item (including persistence instances or attributes) after the SELECT statement, the queried set element is the persistence instance or attribute. If the SELECT statement has multiple items, each set element is an array consisting of multiple items selected. For example:
select p.name,p from Person as p
The Set element obtained by executing the hql statement is similar[String, person]Structure array.
Note:
Even if the list item after select Selects all the attributes of a persistence class, these attributes are still attributes and hibernate will not encapsulate these attributes into objects. Hibernate encapsulates a persistence object only when the alias of the persistence class (actually the Instance name) is provided in the list after the SELECT statement. |
Select also supports storing selected attributes into a list object. For example:
select new list(p.name,p.address) from Person as p
Run the preceding hql statement to obtain a set.
You can even encapsulate the selected attributes into objects. For example:
select new ClassTest(p.name,p.address) from Person as p
The premise is that classtest supports p. name, P. address constructor, if p. the data type of name is string, P. if the data type of address is string, the classtest must have the following constructor:
Classtest (string S1, string S2)
The result returned by executing the preceding hql statement is a set, in which the classtest object is used when the set element is used.
Select also supports alias naming for the selected expression, for example:
select p.name as personName from Person as p
This usage is more common in combination with new map, for example:
select new map(p.name as personName) from Person as p
The result returned by executing the preceding hql statement is a set, where the set element is a map object, using personname as the map key, and the actually selected value as the MAP value.