Use hql in hibernate to query some fields

Source: Internet
Author: User

Hql queries some fields in a single table:

In hibernate, the object class is queried using hql statements, and the returned result of the list method is a list. The objects encapsulated in the list are divided into the following three situations:

1. When you query all fields, such as "from object class", the objects encapsulated in the list are the object class, And all attributes are filled.

2. query only one field. By default, the object is encapsulated in the list.

3. query two or more fields. By default, the list encapsulates object [], and the length is consistent with the number of fields queried.

In the latter two cases, it is not convenient to use the Tag time, because it cannot be directly converted into an object of the object class. The simple solution is:

Use select new package name. Class Name (attribute 1, attribute 2…) in hql ......) From
Object Class. At the same time, add the constructor with parameters to the object class. The number and order of parameters are as follows (attribute 1, attribute 2 ......)
In this way, the objects of the object class are stored in the list, and the queried attributes are filled, making it easier to use.

To query multiple table fields in hql:

Select New package name. Table 1 Object Class Name (table 1. Attribute 1, Table 2. Attribute 2 ......) From table 1 entity class, Table 2 entity class where
Table 1.id= table 2.id( associated fields). At the same time, add the attributes of table 2 and the Construction Method with parameters to the object class of Table 1 to be returned, the number and order of parameters are as follows. attribute 1, table 2. attribute
2 ......) Consistent

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.