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