Hibernate Query Language
Hibernate Query Language (HQL) is an object-oriented query language that is similar to SQL but operates on objects rather than tables. HQL is used to query the database and retrieve data in the form of objects.
Here's an example of how to use HQL to retrieve data from the database:
Session session = sessionFactory.openSession();
Transaction transaction = session.beginTransaction();
String hql = "FROM Employee E WHERE E.salary > 5000";
Query query = session.createQuery(hql);
List<Employee> employees = query.list();
for (Employee employee : employees) {
System.out.println(employee.getName() + " " + employee.getSalary());
}
transaction.commit();
session.close();
In this example, a session is opened, and a transaction is started as described in the previous answer. Then, a query is created using the session.createQuery
method and passing in a string containing the HQL query. The query.list()
method is then called to retrieve a list of Employee
objects that match the query criteria.
The FROM
clause in the HQL query specifies the object to be retrieved, and the WHERE
clause specifies the criteria for retrieving the objects. In this example, the HQL query retrieves all Employee
objects where the salary is greater than 5000.
Finally, the results are printed to the console, and the transaction is committed and the session is closed as described in the previous answer.
Leave a Comment