hibernate HQL

Session session = null;
try {
	session = HibernateSessionFactory.getSessionFactory().getCurrentSession();
	session.beginTransaction();
			
	//FROM clause
	//Simple query, Employee is the entity name instead of the table name in the database (object-oriented feature)  
	//hql = "FROM Employee";  
	//hql = "FROM Employee AS e"; // use alias  
	//hql = "FROM Employee e"; // Use an alias, the as keyword can be omitted  
	String hql = "FROM User";
	Query query = session.createQuery(hql);
	List results = query.list();
	System.out.println(results.size());
			
	//WHERE clause
	// with filter conditions (alias can be used)
	//hql = "FROM Employee WHERE id < 10";  
	//hql = "FROM Employee e WHERE e.id < 10";  
	//hql = "FROM Employee e WHERE e.id < 10 AND e.id > 5";  
	hql = "FROM Role r WHERE r.id = 1";
	query = session.createQuery(hql);
	results = query.list();
	if(results != null & results.size() > 0)
		System.out.println(((Role) results.get(0)).getRoleName());
			
	//ORDER BY clause
	//With sorting conditions:
	//hql = "FROM Employee e WHERE e.id < 10 ORDER BY e.name";  
	//hql = "FROM Employee e WHERE e.id < 10 ORDER BY e.name DESC";  
	//hql = "FROM Employee e WHERE e.id < 10 ORDER BY e.name DESC, e.id ASC";  
	hql = "FROM Role r WHERE r.id > 0 "
		+ "ORDER BY r.id DESC, r.roleName ASC ";
	query = session.createQuery(hql);
	results = query.list();
	System.out.println(results.size());
			
	//Specify the select clause (select * cannot be used)  
	//hql = "SELECT e FROM Employee e"; // 相当于"FROM Employee e"  
	//hql = "SELECT e.name FROM Employee e"; // Only one column is queried, and the element type of the returned collection is the type of this property  
	//hql = "SELECT e.id, e.name FROM Employee e"; // Query multiple columns, the element type of the returned collection is Object array  
	//hql = "SELECT new Employee(e.id, e.name) FROM Employee e"; // You can use the new syntax to specify to encapsulate some of the queried properties into the object  
	hql = "SELECT u.userName FROM User u";
	query = session.createQuery(hql);
	//Paging query
	query.setFirstResult(0);
	query.setMaxResults(10);
	results = query.list();
	for (Object obj : results) {
		String str = (String) obj;
		System.out.println(str);
	}
			
	// Aggregate functions: count(), max(), min(), avg(), sum()  
	//hql = "SELECT COUNT(*) FROM Employee"; // The result returned is Long  
	//hql = "SELECT min(id) FROM Employee"; // The returned result is the type of the id property  

	//GROUP BY  Having
	/**
	hql = "SELECT e.name, COUNT(e.id) FROM Employee e GROUP BY e.name";  
	hql = "SELECT e.name, COUNT(e.id) FROM Employee e GROUP BY e.name " +
		"HAVING count(e.id) > 1";  
	hql = "SELECT e.name, COUNT(e.id) FROM Employee e WHERE id < 9 " +
		"GROUP BY e.name HAVING count(e.id) > 1";  
	hql = "SELECT e.name, COUNT(e.id) FROM Employee e " +
		"WHERE e.id < 9 " +  
		"GROUP BY e.name " +   
		"HAVING count(e.id) > 1 " +
		"ORDER BY count(e.id) ASC";
	hql = "SELECT e.name,COUNT(e.id) AS c FROM Employee e " +
		"WHERE e.id < 9 " +
		"GROUP BY e.name " +
		"HAVING count(e.id) > 1 " + // column aliases cannot be used in the having clause  
		"ORDER BY c ASC"; // Column aliases can be used in the orderby clause  
 	*/
	hql = "SELECT COUNT(*), u.userName FROM User u GROUP BY u.userName ";
	query = session.createQuery(hql);
	results = query.list();
	Object[] arr = (Object[]) results.get(0);
	System.out.println(arr[0] + ", " + arr[1]);
			
	//Connection query
	// HQL is an object-oriented query  
	//>> inner join (inner keyword can be omitted)  
	//hql = "SELECT e.id, e.name, d.name FROM Employee e JOIN e.department d";  
	//hql = "SELECT e.id, e.name, d.name FROM Employee e INNER JOIN e.department d";  
	//>> Left outer join (outer keyword can be omitted)  
	//hql = "SELECT e.id, e.name, d.name FROM Employee e LEFT OUTER JOIN e.department d";  
	//>> Right outer join (outer keyword can be omitted)  
	//hql = "SELECT e.id, e.name, d.name FROM Employee e RIGHT JOIN e.department d";  
	// more convenient method can be used  
	//hql = "SELECT e.id, e.name, e.department.name FROM Employee e";  
		
	// use parameters when querying  
	// >> Method 1: Use '?' placeholder  
	/**
	hql = "FROM Employee e WHERE id BETWEEN ? AND ?";  
	List list = session.createQuery(hql)
		.setParameter(0, 5) // Set the parameter, the index of the first parameter is 0.  
		.setParameter(1, 15)
		.list();  
			
	// >> Method 2: Use variable name  
	hql = "FROM Employee e WHERE id BETWEEN :idMin AND :idMax";  
	List list = session.createQuery(hql)
		.setParameter("idMax", 15)
		.setParameter("idMin", 5)
		.list();  
			
	// When the parameter is a collection, be sure to use setParameterList() to set the parameter value  
	hql = "FROM Employee e WHERE id IN (:ids)";  
	List list = session.createQuery(hql)
		.setParameterList("ids", new Object[] { 1, 2, 3, 5, 8, 100 })
		.list();
	*/
			
	//update and delete, will not notify the Session cache  
	/**
	// >> Update
	int result = session.createQuery(
		"UPDATE Employee e SET e.name = ? WHERE id > 15")
		.setParameter(0, "Anonymous")
		.executeUpdate(); // Returns an int result indicating how many rows were affected.  
	// >> Delete  
	int result1 = session.createQuery(
		"DELETE FROM Employee e WHERE id > 15")
		.executeUpdate(); // Returns an int result indicating how many rows were affected.  
	*/
			
	session.getTransaction().commit();
			
} catch(Exception e) {
	e.printStackTrace ();  
	session.getTransaction().rollback();  
} finally {  
	HibernateSessionFactory.closeSession();  
}

   * HQL: Hibernate Query Language.
  * Features: 
  * >> 1, similar to SQL, the syntax in SQL can basically be used directly. 
  * >> 2, SQL queries tables and columns in tables; HQL queries objects and attributes in objects. 
  * >> 3, HQL keywords are not case sensitive, class names and attribute names are case sensitive. 
  * >> 4, SELECT can be omitted. 

 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326970165&siteId=291194637