Hibernate Framework HQL and Criteria API 1 — Questions and Answers
Question 1: What does HQL stand for in Hibernate?
- Hibernate Query Language (Correct answer)
- Hybrid Query Language
- Hibernate Question Language
- High Query Language
Correct answer: Hibernate Query Language
HQL stands for Hibernate Query Language, an object-oriented query language similar to SQL but operating on entity classes.
Question 2: How does HQL differ from SQL?
- HQL operates on entity class names, SQL operates on table names (Correct answer)
- HQL is faster than SQL
- HQL only supports SELECT, not INSERT
- HQL does not support joins
Correct answer: HQL operates on entity class names, SQL operates on table names
HQL uses Java entity class and field names instead of database table and column names, making queries object-oriented.
Question 3: Which method executes an HQL SELECT query that returns a list of results?
- query.list() (Correct answer)
- query.execute()
- query.fetch()
- query.results()
Correct answer: query.list()
The list() method on a Hibernate Query object executes the HQL query and returns all matching results as a java.util.List.
Question 4: How do you set a named parameter in an HQL query?
- query.setParameter("name", value) (Correct answer)
- query.bind("name", value)
- query.set("name", value)
- query.addParam("name", value)
Correct answer: query.setParameter("name", value)
The setParameter() method binds a value to a named parameter (prefixed with : in HQL) in the query.
Question 5: Which HQL clause is used to filter results, equivalent to SQL WHERE?
- WHERE (Correct answer)
- FILTER
- HAVING
- LIMIT
Correct answer: WHERE
HQL uses the WHERE clause just like SQL to filter query results based on specified conditions.
Question 6: What does the HQL keyword 'fetch' do in a join?
- Eagerly loads the associated entity in the same query (Correct answer)
- Lazily loads the association
- Skips the association
- Counts the associated entities
Correct answer: Eagerly loads the associated entity in the same query
The 'join fetch' clause in HQL tells Hibernate to eagerly initialize the association in the same SQL query.
What does HQL stand for in Hibernate?