1.

What is HQL?

Answer»

Hibernate Query Language (HQL) is used as an extension of SQL. It is very simple, EFFICIENT, and very flexible for performing complex operations on relational databases without writing complicated queries. HQL is the object-oriented representation of query language, i.e instead of using table name, we MAKE use of the class name which makes this language independent of any database.

This makes use of the Query interface provided by Hibernate. The Query object is obtained by calling the createQuery() method of the hibernate Session interface.

Following are the most commonly used methods of query interface:

  • public int executeUpdate() : This method is used to RUN the update/delete query.
  • public List list(): This method returns the result as a list.
  • public Query setFirstResult(int rowNumber): This method ACCEPTS the row number as the PARAMETER using which the record of that row number would be retrieved.
  • public Query setMaxResult(int rowsCount): This method returns a maximum up to the specified rowCount while retrieving from the database.
  • public Query setParameter(int position, Object value): This method sets the value to the attribute/column at a particular position. This method follows the JDBC style of the query parameter.
  • public Query setParameter(String name, Object value): This method sets the value to a named query parameter.

Example: To get a list of all records from InterviewBitEmployee Table:

Query query=session.createQuery("from InterviewBitEmployee"); List<InterviewBitEmployee> list=query.list(); System.out.println(list.get(0));


Discussion

No Comment Found