Hibernate Query Method (ii)

Source: Internet
Author: User

The 1.HQL query (Hibernate query Language) operates on entity classes and attributes

* Check all records

// The query object is used by the 1.HQL lookup operation //   (1) write the SQL statement to create the query object,    String hql = "from Student";    // From behind is the entity class name   Query query = session.createquery ("hql"); // (2) Call method to get results list<student> list = query.list (); // (3) traversing output  for (Student student:list) {  syso (Student.getid ()+ ":::" +student.getname ());}

2. Conditional query

*****1.

//The query object is used by the 1.HQL lookup operation//(1) write the SQL statement to create the query object,String hql = "from Student where id=? and name=? "; Query Query= Session.createquery ("HQL");//set a condition value//What is the first parameter? The position type is int the second parameter is the set valueQuery.setparameter (0,1); Query.setparameter (1, "Zhang San");//(3) Call method to get resultslist<student> list =query.list ();//(4) Traversing output for(Student student:list) {syso (Student.getid ()+":::"+student.getname ());}

2. Fuzzy condition Query

//The query object is used by the 1.HQL lookup operation//(1) write the SQL statement to create the query object,String HQL ="from Student where is name like?""; Query Query= Session.createquery ("HQL");//set a condition value//What is the first parameter? The position type is int the second parameter is the set value//query for names that begin with ZhangQuery.setparameter(0, "Zhang%");//(3) Call method to get resultslist<student> list =query.list ();//(4) Traversing output for(Student student:list) {syso (Student.getid ()+":::"+student.getname ());}

3. Sort Queries

// The query object is used by the 1.HQL lookup operation //   (1) write the SQL statement to create the query object, Ascending query ASC descending query desc;     ORDER BY CID ASC " ;      = Session.createquery ("hql"); // (3) Call method to get results list<student> list = query.list (); // (4) Traversing output  for (Student student:list) {  syso (Student.getid ()+ ":::" +student.getname ());}

4. Paging Query

implementing paging using the keyword limit in MySQL

SELECT * from Student  LIMIT 0,5//0 is the starting position 5 is the maximum number of records displayed

Two methods are encapsulated in hibernate to implement paged query operations

// The query object is used by the 1.HQL lookup operation //   (1) Write SQL statement query all create query object,    String hql = "from Student";      = Session.createquery ("hql"); //Set paging data query.setfirstresult (0); Query.setmaxresults (5); // (3) Call method to get results list<student> list = query.list (); // (4) Traversing output  for (Student student:list) {  syso (Student.getid ()+ ":::" +student.getname ());}

5. Projection Query

Query for all values of a field in a table

// The query object is used by the 1.HQL lookup operation //   (1) write the SQL statement to create the query object,    String hql = "Select  name from Student";      = Session.createquery ("hql"); // (3) Call the method to get the type of the result generic here can be a string if the type of multiple fields is not the same as can be written objectlist<object> list = query.list (); // (4) Traversing output  for (Object object:list) {  Syso (object);}

6. Aggregation function

Several common aggregation functions are count avg min Max sum;

Show the number of records in the query table here

// The query object is used by the 1.HQL lookup operation //   (1) write the SQL statement to create the query object,    String hql = "Select COUNT (*) from Student";      = Session.createquery ("hql"); // (3) Call method to get results here, using the Query object method directly returns the object form without a list Object obj =  query.uniqueresult (); SYSO (obj); <--Note that the above method return type is object if you want to return an int or other type  we first consider a strong turn of the type, but here we will output an exception  . The exception type is a type conversion exception (ClassCastException) so we will first convert the object type to a long type in the conversion to an integer type; long LongO = (long) obj; int count = Longo.intvalue (); Syso (count);  -

Hibernate Query Method (ii)

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.