Hibernate Interview Questions and Answers - 4
31) What does it mean to be inverse?
A) It informs hibernate to ignore that end of the relationship. If the one–to–many was marked as inverse, hibernate would create a child–>parent relationship (child.getParent).
32) What do you mean by Named – SQL query?
A) Named SQL queries are defined in the mapping xml document and called wherever required.
Example:
SELECT emp.EMP_ID AS {emp.empid},
emp.EMP_ADDRESS AS {emp.address},
emp.EMP_NAME AS {emp.name}
FROM Employee EMP WHERE emp.NAME LIKE :name
Invoke Named Query :
List people = session.getNamedQuery(“empdetails”).setString(“TomBrady”, name).setMaxResults(50).list();
33) How do you invoke Stored Procedures?
A) { ? = call selectAllEmployees() }
34) Explain Criteria API?
A) Criteria is a simplified API for retrieving entities by composing Criterion objects. This is a very convenient approach for functionality like “search” screens where there is a variable number of conditions to be placed upon the result set.
Example :
List employees = session.createCriteria(Employee.class)
.add(Restrictions.like(“name”, “a%”) )
.add(Restrictions.like(“address”, “Boston”))
.addOrder(Order.asc(“name”) )
.list();
35) Define HibernateTemplate?
A) org.springframework.orm.hibernate.HibernateTemplate is a helper class which provides different methods for querying/retrieving data from the database. It also converts checked HibernateExceptions into unchecked DataAccessExceptions.
36) What are the benefits does HibernateTemplate provide?
A) The benefits of HibernateTemplate are :
* HibernateTemplate, a Spring Template class simplifies interactions with Hibernate Session.
* Common functions are simplified to single method calls.
* Sessions are automatically closed.
* Exceptions are automatically caught and converted to runtime exceptions.
37) How do you switch between relational databases without code changes?
A) Using Hibernate SQL Dialects , we can switch databases. Hibernate will generate appropriate hql queries based on the dialect defined.
38) If you want to see the Hibernate generated SQL statements on console, what should we do?
A) In Hibernate configuration file set as follows:
39) What are derived properties?
A) The properties that are not mapped to a column, but calculated at runtime by evaluation of an expression are called derived properties. The expression can be defined using the formula attribute of the element.
40) What is component mapping in Hibernate?
A)
* A component is an object saved as a value, not as a reference
* A component can be saved directly without needing to declare interfaces or identifier properties
* Required to define an empty constructor
* Shared references not supported
Reference Books:
A) It informs hibernate to ignore that end of the relationship. If the one–to–many was marked as inverse, hibernate would create a child–>parent relationship (child.getParent).
32) What do you mean by Named – SQL query?
A) Named SQL queries are defined in the mapping xml document and called wherever required.
Example:
SELECT emp.EMP_ID AS {emp.empid},
emp.EMP_ADDRESS AS {emp.address},
emp.EMP_NAME AS {emp.name}
FROM Employee EMP WHERE emp.NAME LIKE :name
Invoke Named Query :
List people = session.getNamedQuery(“empdetails”).setString(“TomBrady”, name).setMaxResults(50).list();
33) How do you invoke Stored Procedures?
A) { ? = call selectAllEmployees() }
34) Explain Criteria API?
A) Criteria is a simplified API for retrieving entities by composing Criterion objects. This is a very convenient approach for functionality like “search” screens where there is a variable number of conditions to be placed upon the result set.
Example :
List employees = session.createCriteria(Employee.class)
.add(Restrictions.like(“name”, “a%”) )
.add(Restrictions.like(“address”, “Boston”))
.addOrder(Order.asc(“name”) )
.list();
35) Define HibernateTemplate?
A) org.springframework.orm.hibernate.HibernateTemplate is a helper class which provides different methods for querying/retrieving data from the database. It also converts checked HibernateExceptions into unchecked DataAccessExceptions.
36) What are the benefits does HibernateTemplate provide?
A) The benefits of HibernateTemplate are :
* HibernateTemplate, a Spring Template class simplifies interactions with Hibernate Session.
* Common functions are simplified to single method calls.
* Sessions are automatically closed.
* Exceptions are automatically caught and converted to runtime exceptions.
37) How do you switch between relational databases without code changes?
A) Using Hibernate SQL Dialects , we can switch databases. Hibernate will generate appropriate hql queries based on the dialect defined.
38) If you want to see the Hibernate generated SQL statements on console, what should we do?
A) In Hibernate configuration file set as follows:
<property name="show_sql">true</property>
39) What are derived properties?
A) The properties that are not mapped to a column, but calculated at runtime by evaluation of an expression are called derived properties. The expression can be defined using the formula attribute of the element.
40) What is component mapping in Hibernate?
A)
* A component is an object saved as a value, not as a reference
* A component can be saved directly without needing to declare interfaces or identifier properties
* Required to define an empty constructor
* Shared references not supported
Reference Books: