hibernate -- HQL query summary

Posted by JakeJ on Sun, 26 Apr 2020 16:09:42 +0200

hibernate -- HQL query summary

HQL overview:

HQL(Hibernate Query Language) is an object-oriented query language, which is similar to SQL query language. Among various search methods provided by Hibernate, HQL is the most widely used one

list: multiple query results
uniqueResult: unique query result

Simple query of HQL

// Simple query:
String hql = "from Customer";
//Method 1
Query query = session.createQuery(hql);
List<Customer> list = query.list();
//Method 2
List<Customer> list = session.createQuery(hql).list();

Alias query for HQL:

// Alias query:
//String hql = "from Customer asc";
//String hql = "from Customer c";
String hql = "select c from Customer c";

HQL polymorphic query:

String hql = "from java.lang.Object";
String hql = "from java.io.Serializable";

Sort query for HQL:

String hql = "from Customer order by age asc";
String hql = "from Customer order by age desc";

Paging query for HQL:

Query query = session.createQuery("from Order");
query.setFirstResult(10);
query.setMaxResults(10);
List<Order> list = query.list();

HQL retrieves a single object:

Query query = session.createQuery("from Customer where cname = ?");
query.setParameter(0, "Zhang San");
Customer customer = (Customer) query.uniqueResult();

Condition query of HQL:

/**
* HQL Condition query * bind parameters by location:
*/
// 1. Query by name
List<Customer> list = session
		.createQuery(
				"from Customer where cname = ?")
		.setParameter(0,"Zhang San").list(); 

System.out.println(list.get(0));

// 2. Query by name
List<Customer> list = session
		.createQuery(
				"from Customer c where c.cname = ?") 
		.setParameter(0,"Zhang San").list(); 

System.out.println(list.get(0));

// 3. Condition parallel:
List<Customer> list = session
		.createQuery(
				"from Customer where cname like ? and age >= ?")
		.setParameter(0, "let%").setParameter(1, 35).list();

for (Customer customer : list) {
	System.out.println(customer);
}

/**
 * HQL Condition query * bind parameters by name:
 */
List<Customer> list = session
		.createQuery(
				"from Customer where cname like :cname and age >= :age")
		.setParameter("cname", "let%").setParameter("age", 35).list();
		
for (Customer customer : list) {
	System.out.println(customer);
}

HQL uses aggregate functions:

//count()/max()/min()/avg()/sum()
String hql = "select count(*) from Order";
Long num = (Long) session.createQuery(hql).uniqueResult();

HQL group statistics:

String hql = "select o.customer.cname,count(*) from Order o group by o.customer.cname";
List<Object[]> list = session.createQuery(hql).list();
for (Object[] objects : list) {
	System.out.println(Arrays.toString(objects));
}

Projection query of HQL:

String hql = "select cname from Customer";
List<String> list  = session.createQuery(hql).list();
for (String cname : list) {
	System.out.println(cname);
}
		
String hql = "select age from Customer";
List<Integer> list  = session.createQuery(hql).list();
for (Integer age : list) {
	System.out.println(age);
}

String hql= "select cname,age from Customer";
List<Object[]> list  = session.createQuery(hql).list();
for (Object[] objects : list) {
	System.out.println(Arrays.toString(objects));
}

// Use constructor to query projection
String hql = "select new Customer(cname,age) from Customer";
List<Customer> list = session.createQuery(hql).list();
for (Customer customer : list) {
	System.out.println(customer);
}

Topics: Session Hibernate Java SQL