SQL properties |
Description |
FROM |
We use FROM when we want to load the persistend object into the memory. With the help of the following syntex we can understand what is the mean of tha above line:
String hql = "FROM Hibernate_HQL"; Query query = session.createQuery(hql); List results = query.list();
When we use a class thorough a package then we need to fully qualify a class name in HQL, just specify the package and class name as follows:
String hql = "FROM r4r.hibernate.criteria.Hibernate_HQL"; Query query = session.createQuery(hql); List results = query.list(); |
AS |
In the SQL AS keyword perform the common action means it define the relation two table. But in HQL it can be used to assign to the classes for the HQL qeries.
For Understand the above discussion we need to go through with the example which is as given above:
String hql = "FROM Hibernate_HQL AS HQ"; Query query = session.createQuery(hql); List results = query.list();
In the HQL AS keyword is optional. We can specify without using the AS keyword. With the help of the AS keyword we can specify the alias directly after the class name.
We can see in the below example:
String hql = "FROM Hibernate_HQL HQ"; Query query = session.createQuery(hql); List results = query.list(); |
SELECT |
The SELECT is used to fetch tables from the database which provides more control over the result set than the from. When we want to obtain few properties of objects instead of the complete object, for this we can use the SELECT clause.
We have given below simple syntax of using SELECT clause to get just first_name field of the Employee object:
String hql = "SELECT HQ.firstName FROM Hibernate_HQL HQ"; Query query = session.createQuery(hql); List results = query.list();
We can understand the given above line. we can say that it is notable here that Hibernate_HQL.firstName is a property of Hibernate_HQL object rather than a field of the Hibernate_HQL table. |
WHERE |
WHERE is used in the HQL When we want to narrow the specific objects that are returned from storage.
With the help of the Following simple syntax of using WHERE clause:
String hql = "FROM Hibernate_HQL HQ WHERE HQ.id = 10"; Query query = session.createQuery(hql); List results = query.list(); |
ORDER BY |
When we want to sort HQL query's results, then we need to use the ORDER BY clause. With the help we can order the results by any property on the objects in the result set either ascending (ASC) or descending (DESC).
Given below simple syntax of using ORDER BY clause:
String hql = "FROM Hibernate_HQL HQ WHERE HQ.id > 10 ORDER BY HQ.salary DESC"; Query query = session.createQuery(hql); List results = query.list();
When ever we want to sort by more than one property, then we need to add additional properties to the end of the order by clause, separated by commas as follows:
String hql = "FROM Hibernate_HQL HQ WHERE HQ.id > 10 " + "ORDER BY HQ.firstName DESC, HQ.salary DESC "; Query query = session.createQuery(hql); List results = query.list(); |
GROUP BY |
GROUP BY tells us that the Hibernate pull information from the database and group it based on a value of an attribute and, With the help we can find the aggregate value.
Given simple syntax of using GROUP BY clause:
String hql = "SELECT SUM(HQ.salary), HQ.firtName FROM Hibernate_HQL HQ " + "GROUP BY E.firstName"; Query query = session.createQuery(hql); List results = query.list(); |
Using Named |
Hibernate supports named parameters in its HQL queries. This makes writing HQL queries that accept input from the user easy and you do not have to defend against SQL injection attacks.
Following is the simple syntax of using named parameters:
String hql = "FROM Hibernate_HQL HQ WHERE HQ.id = : Hibernate_HQL_id"; Query query = session.createQuery(hql); query.setParameter("employee_id",10); List results = query.list(); |
UPDATE |
Bulk updates are new to HQL with Hibernate 3, and deletes work differently in Hibernate 3 than they did in Hibernate 2. The Query interface now contains a method called executeUpdate() for executing HQL UPDATE or DELETE statements.
The UPDATE clause can be used to update one or more properties of an one or more objects. Following is the simple syntax of using UPDATE clause:
String hql = "UPDATE Hibernate_HQL set salary = : salary " + "WHERE id = :employee_id"; Query query = session.createQuery(hql); query.setParameter("salary", 1000); query.setParameter("Hibernate_HQL_id", 10); int result = query.executeUpdate(); System.out.println("Rows affected: " + result); |
DELETE |
The DELETE clause can be used to delete one or more objects. Following is the simple syntax of using DELETE clause:
String hql = "DELETE FROM Hibernate_HQL " + "WHERE id = : Hibernate_HQL_id"; Query query = session.createQuery(hql); query.setParameter("Hibernate_HQL_id", 10); int result = query.executeUpdate(); System.out.println("Rows affected: " + result); |
INSERT INTO |
HQL supports INSERT clause only where records can be inserted from one object to another object. Following is the simple syntax of using INSERT INTO clause:
String hql = "INSERT INTO Employee(firstName, lastName, salary)" + "SELECT firstName, lastName, salary FROM old_employee_Hibernate_HQL"; Query query = session.createQuery(hql); int result = query.executeUpdate(); System.out.println("Rows affected: " + result); |
Functions |
Description |
avg(property name) |
The average of a property's value |
count(property name or *) |
The number of times a property occurs in the results |
max(property name) |
The maximum value of the property values |
min(property name) |
The minimum value of the property values |
sum(property name) |
The sum total of the property values |
Method |
Description |
Query setFirstResult(int startPosition) |
This method takes an integer that represents the first row in your result set, starting with row 0. |
Query setMaxResults(int maxResult) |
This method tells Hibernate to retrieve a fixed number maxResults of objects. |