Object Query
Example 1: hql Code
- String hql = "from user ";
- 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
- From user where user. Age =20;
From user where user. Age = 20;
Example 2 (return an attribute): hql code
- String hql = "select C. customernamefrom customer C";
- Query 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 );
- }
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.
-
- String hql = "select C. customerid, C. customername" +
-
- "From customer C";
-
- Query query = session. createquery (hql );
-
- Iteratorit = query. List (). iterator ();
-
- While (it. hasnext ()){
-
- Object [] OBJ = (object []) it. Next (); // list = (list) it. Next ();
-
- Long id = (long) OBJ [0]; // Long id = (long) list. Get (0);
- String name = (string) OBJ [1]; // String name = (string) list. Get (1);
-
- System. Out. println (Id. longvalue () +""+ Name );
-
- }