005 --- preliminary query interface

Source: Internet
Author: User

Query session. createquery (string hql)Method;

* The session. createquery () method of hibernate uses the hql (Hibernate query statement) statement to query objects.

* Hql: it is the query object, for example, "from user", where from is not case sensitive, while user is case sensitive because it is an object. Yesuser class

* Returns the query object.

* After this statement is executed, Hibernate automatically generates the SQL statement of the corresponding database based on the database adapter configured in the configuration file. For example:

 

Hibernate: Select user0 _. ID as id0 _, user0 _. name as name0 _, user0 _. password as password0 _, user0 _. createtime as createtime0 _, user0 _. expiretime as expiretime0 _ from user user0 _

 

QueryPaging query:

 

The complete code is as follows:

Public void testquery1 (){
Session session = NULL;

Try {
Session = hibernateutils. getsession ();
Session. begintransaction ();

Query query = session. createquery ("from user ");

// Query by PAGE
Query. setfirstresult (0); // from which record the query starts, which is calculated from 0
Query. setmaxresults (2); // number of records displayed per page.
/*
* The query object has a list () method, which automatically generates List objects for all queried objects and returns them.
*/
List userlist = query. List ();

// Then we can display the data.
For (iterator iter = userlist. iterator (); ITER. hasnext ();){
User user = (User) ITER. Next ();
System. Out. Print (user. GETID () + "");
System. Out. println (user. getname ());
}

Session. gettransaction (). Commit ();
} Catch (hibernateexception e ){
E. printstacktrace ();
Session. gettransaction (). rollback ();
} Finally {
Hibernateutils. closesession (session );
}
}

 

005 --- preliminary query interface

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.