There are many ways to query hql, and yesterday came into contact with a query method based on the Pojo class, summarized here:
Usually we can use simple "from [POJO OBJ]" syntax to assemble a simple hql query. If we want to specify a field, we can use the syntax of the new Pojo object, as follows:
The following article uses Weiboaccountbean to refer to Pojo objects:
Select new Weiboaccountbean (Bean.accountid,bean.expire,bean.token,bean.companyid,bean.userid, Bean.accountemail,bean.accountname,bean.accountpassword,bean.status,bean.openurl,bean.isenable,bean.source) From Weiboaccountbean Bean where 1=1
The HQL can use the query object produced by the Session.createquery method to execute the list method, as follows:
Query query = This.gethibernatetemplate (). Getsessionfactory (). Getcurrentsession (). CreateQuery (HQL);
Query.list ();
The Pojo class must be combined with the following construction methods:
Public Weiboaccountbean (Integer accountid,string expire,string token,integer CompanyID, String userid,string accountemail,string accountname,string accountpassword,integer status,string openurl,string isenable,string source) {
this.accountid=accountid;
This.expire=expire;
This.token=token;
This.companyid=companyid;
This.userid=userid;
This.accountemail=accountemail;
This.accountname=accountname;
This.accountpassword=accountpassword;
This.status=status;
This.openurl = OPENURL;
this.isenable = isenable;
This.source = source;
}