Object Query using hql

Source: Internet
Author: User
Object Query
Example 1: hql Code
    1. String hql = "from user ";
    2. List list = session. createquery (hql). List ();
 
String hql = "from user"; List list = session. createquery (hql). List ();

Because hql statements are similar to standard SQL statements, we can also use the WHERE clause in hql statements, and use various expressions, comparison operators, and "and" in the WHERE clause ", or connects a combination of different query conditions. Take a look at some simple examples below: hql code

    1. From user where user. Age =20;
 
From user where user. Age = 20;

Example 2 (return an attribute): hql code

    1. String hql = "select C. customernamefrom customer C";
    2. Query query = session. createquery (hql );
    3. Iteratorit = query. List (). iterator ();
    4. System. Out. println (query. List (). Size ());
    5. While (it. hasnext ()){
    6. String c = (string) it. Next ();
    7. System. Out. println (C );
    8. }
 
String hql = "select C. customernamefrom customer C "; query = session. createquery (hql); iteratorit = query. list (). iterator (); system. out. println (query. list (). size (); While (it. hasnext () {string c = (string) it. next (); system. out. println (c );}

Example 3 (multiple attributes are returned ):
If multiple attributes are returned, they are loaded into an array or hql code in the collection.

  1. String hql = "select C. customerid, C. customername" +
  2. "From customer C";
  3. Query query = session. createquery (hql );
  4. Iteratorit = query. List (). iterator ();
  5. While (it. hasnext ()){
  6. Object [] OBJ = (object []) it. Next (); // list = (list) it. Next ();
  7. Long id = (long) OBJ [0]; // Long id = (long) list. Get (0);
  8. String name = (string) OBJ [1]; // String name = (string) list. Get (1);
  9. System. Out. println (Id. longvalue () +""+ Name );
  10. }

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.