Hibernate HQL Query the specified field and get the result set

Source: Internet
Author: User

In Hibernate, the entity class is queried with the HQL statement, and the return result of the list method is a list, and the objects encapsulated in the list are divided into the following three cases:
1. In the case of querying all fields, such as from entity class, the objects encapsulated in list are the entity classes themselves, and each property will be populated.
2. Query only one field, by default, the object is encapsulated in list.
3. Query two or more fields, by default, the list is encapsulated by object[], and the length is the same as the number of fields queried.

For the latter two cases, it is not convenient to use a label traversal because you cannot convert directly to an object of an entity class. A relatively simple solution is to:

A: Use the select New package name in Hql. Class name (attribute 1, property 2 ...) from entity class, and add the constructor method with parameter in the entity class, the number and order of parameters with (attribute 1, property 2 ...) Consistent so that the list that we get is still the object of the entity class, and the queried properties are populated, which is more convenient to use.

の:HQL Query multiple table section fields, select New package name. Table 1 entity class name (table 1. Attribute 1, table 2. Property 2 ...) from table 1 entity class, Table 2 entity class where table 1.id= table 2. ID (that is, the associated field), add the properties of table 2 and the constructor of the parameter to the table 1 entity class to be returned, with the number and order of the parameters (table 1. Property 1, Table 2. Property 2 ...) Stay consistent

For example, to query for Pid,score,title,totalaccept,totalsubmission,unsee in problem

public class Problem {     private int pid      private int score;      PRI vate int timelimit;      private int memorylimit;      private int totalaccept;      private int totalsubmission;      private int unsee;      Private String title;      Private String description;      private String input;      private String output;            public problem (int pid, int score,string title, int totalaccept, int totalsu Bmission,               int unsee) {         super () &N bsp;        this.pid = pid;          This.score = score;          this.totalaccept = totalaccept;          this.totalsubmission = totalsubmission;          This.unsee = Unsee;          this.title = title;     }     //Omit getter and setter   }   query statements are as follows       query Query=s Ession.createquery ("Select New Problem (Pid,score,title,totalaccept,totalsubmission,unsee) from Problem order by PID") ;         //query.setfirstresult (Firstresult); Paging functions          //query.setmaxresults (MAXRESUTL);                 list<problem> problems=query.list ();// Return or Problem object  









Questions about Hibernate:
I got a piece of it.
Hql= "Select S.id,s.name,t.id,t.name from User s,useraddress t where t.id=s.id"

The SQL inside the user and useraddress is two entity classes, and now the combination of queries out of the two entity class two fields inside, and then I want to create an entity class result, which defines the four result set inside the field, can execute this hql, The result set corresponds to the entity class result inside, result this entity class, did not write the mapping file Result.hbm.xml.
I hope I can help.

2 Ways of doing
Create a class Temp
has attribute Sid,name,tid,sname,tname
Create a constructor function
Public Temp (Sid,name,tid,sname,tname)
{

}
In 1.HQL
List<temp>

Select New Temp (s.id,s.name,t.id,t.name) from User s,useraddress T where t.id=s.id

2.List
Each row of the record is object[] traversal
Object[0] ==s.id
OBJECT[1] ==s.name
OBJECT[2] ==t.id
OBJECT[3] ==t.name





Thanks to the Glamey brother's article, just solved the current problems encountered. The original link is as follows: http://glamey.iteye.com/blog/721019
Suppose we now have a DTO whose properties include the properties of two tables, we now need to convert the contents of the SQL statement query to a DTO object, which is resolved as follows:

String sql = "Select U.username as UserName, P.title as title, P.addtime as Addtime from user as u,post as P where u.id=p. UserId "Query q = factory.getcurrentsession (). createsqlquery (SQL). Setresulttransformer (Transformers.aliastobean ( Postvo.class));

The contents after as in the select above must be the same as the property name in Postvo, so that a collection for Postvo can be returned.
In fact, we can see hibernate this part of the source code will be found, mainly using Aliastobeanresulttransformer this class, through the SQL query, will return the array, and hibernate according to the data table mapping, Automatically help us to set the corresponding field properties, so the red part must be with the attribute value in Vo has been, or will error.
You can also rewrite this class if you need to. such as Voresulttransformer. Then change in DAO to:

Setresulttransformer (New Voresulttransformer (Postvo.class));
In addition, in addition to the above Glamey method, there is also a method:
Query q = Session.createquery ("Select New Com.hibernate.MsgInfo (M.id, M.cont, M.topic.title, M.topic.category.name) From MSG M "); List<msginfo> list=q.list ();
Among them, Msginfo is a dto. It is worth noting that the DTO in the second method must provide a constructor with parameters, and the position of the property in the HQL statement corresponds to position one by one in the construction method.

Hibernate HQL Query the specified field and get the result set (GO)

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.