Copy codeThe Code is as follows: package com. zxd. test;
Import java. util. List;
Import org. hibernate. HibernateException;
Import org. hibernate. Query;
Import org. hibernate. Session;
Import org. hibernate. SessionFactory;
Import org. hibernate. cfg. Configuration;
Import com. zxd. bean. House;
Import com. zxd. util. QueryProperty;
/**
* HQL closed query test class
* @ Author zhang
*
*/
Public class TestHouse {
Public static void main (String [] args ){
// Public member variables
SessionFactory sf = null;
Session session = null;
QueryProperty qp = new QueryProperty ();
// Encapsulate the queried data
Qp. setTitle ("% haofang % ");
Qp. setStreet_id ("1002 ");
Qp. setType_id ("1004 ");
Qp. setLow_price (20 );
Qp. setHigh_price (200 );
Qp. setSmall_floorage (50 );
Qp. setBig_floorage (180 );
// HQL statement
StringBuffer sb = new StringBuffer ();
Sb. append ("from House where ");
Sb. append ("(title like: title )");
Sb. append ("and (type_id like: type_id )");
Sb. append ("and (street_id like: street_id )");
Sb. append ("and (price between: low_price and: high_price )");
Sb. append ("and (floorage between: small_floorage and: big_floorage )");
Try {
// Start Query
Sf = new Configuration (). configure (). buildSessionFactory ();
Session = sf. openSession ();
Query query = session. createQuery (sb. toString ());
Query. setProperties (qp );
List <House> list = query. list ();
// First use: loop
/* For (House house: list ){
System. out. println ("title:" + house. getTitle ());
System. out. println ("Area:" + house. getFloorage ());
System. out. println ("Price:" + house. getPrice ());
System. out. println ("zone:" + house. getStreet (). getDistrict (). getName ());
System. out. println ("the street is:" + house. getStreet (). getName ());
System. out. println ("----------------------------------");
}*/
// The second loop
For (int I = 0; I <list. size (); I ++ ){
System. out. println ("title:" + list. get (I). getTitle ());
System. out. println ("Area:" + list. get (I). getFloorage ());
System. out. println ("Price:" + list. get (I). getPrice ());
System. out. println ("zone:" + list. get (I). getStreet (). getDistrict (). getName ());
System. out. println ("the street is:" + list. get (I). getStreet (). getName ());
System. out. println ("----------------------------------");
}
} Catch (HibernateException e ){
// TODO Auto-generated catch block
E. printStackTrace ();
} Finally {
Session. close ();
Sf. close ();
}
}
}
The first loop in the above example is not remembered, and the key character ":" is used. Generally, this loop is used to traverse a set (List <House>, Map) is very convenient.
The second type of loop is a normal loop. This is the most common for loop in Java.