6.4.4: Select clause of hql Query

Source: Internet
Author: User

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.

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.