HQL basic Query simple Query result for output and Iterator output, hqliterator
The first HQL course:
Hibernate Query Language: hibernate Query Language
Syntax:
Query:
String hql = "from dept ";
Query query = session. createQuery (hql); // returns the list set.
Query is an object with many methods. The. list () method returns a set;
Detailed explanation of the next hql statement. This is a simple operation!
1 public static List <Dept> getDept () {2 List <Dept> list = new ArrayList <Dept> (); 3 Configuration conf = null; 4 SessionFactory factory = null; 5 Session session = null; 6 7 try {8 9 conf = new Configuration (). configure (); 10 // create sessionfactory11 factory = conf. buildSessionFactory (); 12 // open session 13 session = factory. openSession (); 14 // hql15 String hql = "select dept from Dept as dept where dept. deptname like '% sent %' "; 16 Query query = session. createQuery (hql); 17 // list = query. list (); 18 // for (Dept dep: list) {19 // System. out. println (dep); 20 // 21 //} 22 System. out. println ("Iterator"); 23 Iterator <Dept> ite = query. iterate (); 24 while (ite. hasNext () {25 System. out. println (ite. next (); 26 27} 28 29 30} catch (Exception e) {31 e. printStackTrace (); 32 33} finally {34 if (session! = Null) {35 session. close (); 36 37} 38} 39 40 41 42 return list; 43 44}