HQL is the abbreviation of Hibernate Query Language, which provides more flexible and powerful query capabilities; HQL is closer to SQL query syntax.
query
Query all (persistent data)
// s: aliases must be aliased to objects, not *. Not in hibernate* String hql="select s from Student s"; //The Query object is the Query object Query query=session.createQuery(hql); //. list a collection of lists found by. List List<Student> list=query.list(); for (Student s : list) { System.out.println(s.getName()); }
Specify column name to query all (non persistent data)
Sometimes there may be more than ten columns of data in the database, but only a few columns are needed, so you need to specify the column name to query.
//Query the non persistent state of the specified column name //Non persistent: because the data queried does not correspond to the database //s.sid,s.name the alias of the specified column name. Column name String hql="select s.sid,s.name from Student s"; Query query=session.createQuery(hql); //The query is an object array, not a student object List<Object[]> list = query.list(); for (Object[] objects : list) { System.out.println(objects[0]+"\t"+objects[1]); }
Specify column name to query all (persistent data)
//Query the persistent state of the specified column name //In the database, there are corresponding, in the student object //new Student(s.sid,s.name) ------ the constructor corresponding to it in entity class String hql="select new Student(s.sid,name) from Student s"; Query query=session.createQuery(hql); //The Student object is found. List<Student> list=query.list(); for (Student student : list) { System.out.println(student.getName()); }
Function query
//Function query - Maximum //Max (alias. Column name) String hql="select max(s.sid) from Student s"; Query query=session.createQuery(hql); //Return is the only result int max=(Integer) query.uniqueResult(); System.out.println("Maximum value"+max);
Note: when querying the total number of rows, you need to use the long type to receive
Placeholder query
The first way is to get the placeholder Name: user defined name.
//: name custom name String hql="select s from Student s where s.name like :name"; //Set the placeholder's value setparameter (placeholder name (not required:, assignment)); no order Query query=session.createQuery(hql).setParameter("name", "%xx%"); List<Student> list = query.list(); for (Student student : list) { System.out.println(student.getName()); }
The second: through? placeholder
//If the hibernate version is higher than 5, the order of placeholders should be added after? //For example, s.name like? 0 and s.age >? 1 String hql="select s from Student s where s.name like ?0"; //When assigning a value, you need to start from 0. Query query=session.createQuery(hql).setParameter(0, "%x%"); List<Student> list = query.list(); for (Student student : list) { System.out.println(student.getName()); }
Paging query
//Find all String hql="select s from Student s "; int pages=1;//Current number of pages int pageSize=5;//How many lines are displayed on one page //setMaxResults sets the maximum number of pages to display setfirstresult sets which page to start from Query query=session.createQuery(hql).setMaxResults(pageSize).setFirstResult((pages-1)*pageSize); List<Student> list = query.list(); for (Student a : list) { System.out.println(a.getName()); }
There are connection queries below (I use a one to many relationship)
//Connection query (full connection) //C.province.pid PID PID in the province object in C and PID in the p object String sql="select c from City c inner join Province p on c.province.pid=p.pid"; Query query = session.createQuery(sql); List<City> list = query.list(); for (City c : list) { System.out.println(c.getCname()); }
Child connection
//Query the names of all provinces with Han in the city String hql="select p from Province p where p.pid in(select c.province.pid from City c where c.cname like :cname)"; Query query = session.createQuery(hql).setParameter("cname", "%Chinese%"); List<Province> list=query.list(); for (Province province : list) { System.out.println(province.getPname()); }
Child connection 2
//Query the names of all provinces with lakes in cities - the query criteria of one table is the query result of another table String hql="select p from Province p where p.pid in(select c.province.pid from City c where c.cname like :cname)"; Query query = session.createQuery(hql).setParameter("cname", "%Chinese%"); List<Province> list=query.list(); for (Province province : list) { System.out.println(province.getPname()); }