Hibernate Interview Questions and Answers
Question - 61 : - What is a Hibernate Template class?
Answer - 61 : -
When you integrate Spring and Hibernate, Spring ORM provides two helper classes – HibernateDaoSupport and HibernateTemplate. The main reason to use them was to get two things, the Session from Hibernate and Spring Transaction Management. However, from Hibernate 3.0.1, you can use the SessionFactory getCurrentSession() method to get the current session. The major advantage of using this Template class is the exception translation but that can be achieved easily by using @Repository annotation with service classes.
Question - 62 : - What are the collection types in Hibernate?
Answer - 62 : -
There are five collection types in hibernate used for one-to-many relationship mappings.
Bag
Set
List
Array
Map
Question - 63 : - Discuss the Collections in Hibernate
Answer - 63 : -
Hibernate provides the facility to persist the Collections. A Collection basically can be a List, Set, Map, Collection, Sorted Set, Sorted Map. java.util.List, java.util.Set, java.util.Collection, etc, are some of the real interface types to declared the persistent collection-value fields. Hibernate injects persistent Collections based on the type of interface. The collection instances generally behave like the types of value behavior.
Question - 64 : - What are the key components of a Hibernate configuration object?
Answer - 64 : -
The configuration provides 2 key components, namely:
- Database Connection: This is handled by one or more configuration files.
- Class Mapping setup: It helps in creating the connection between Java classes and database tables.
Question - 65 : - What is meant by Hibernate tuning?
Answer - 65 : -
Optimizing the performance of Hibernate applications is known as Hibernate tuning.
The performance tuning strategies for Hibernate are:
- SQL Optimization
- Session Management
- Data Caching
Question - 66 : - What is Transaction Management in Hibernate? How does it work?
Answer - 66 : -
Transaction Management is a property which is present in the Spring framework. Now, what role does it play in Hibernate?
Transaction Management is a process of managing a set of commands or statements. In hibernate, Transaction Management is done by transaction interface. It maintains abstraction from the transaction implementation (JTA, JDBC). A transaction is associated with Session and is instantiated by calling session.beginTransaction().
Question - 67 : - How do you integrate Hibernate with Struts2 or Servlet web applications?
Answer - 67 : -
You can integrate any Struts application with Hibernate. There are no extra efforts required.
- Register a custom ServletContextListener.
- In the ServletContextListener class, first, initialize the Hibernate Session, store it in the servlet context.
- Action class helps in getting the Hibernate Session from the servlet context, and perform other Hibernate task as normal.
Question - 68 : - What are the different states of a persistent entity?
Answer - 68 : -
It may exist in one of the following 3 states:
- Transient: This is not associated with the Session and has no representation in the database.
- Persistent: You can make a transient instance persistent by associating it with a Session.
- Detached: If you close the Hibernate Session, the persistent instance will become a detached instance.
Question - 69 : - How can the primary key be created by using Hibernate?
Answer - 69 : -
A Primary key is a special relational database table column designated to uniquely identify all table records. It is specified in the configuration file hbm.xml. The generator can also be used to specify how a Primary key can be created in the database.
Question - 70 : - Explain about Hibernate Proxy and how it helps in Lazy loading?
Answer - 70 : -
- Hibernate uses a proxy object in order to support Lazy loading.
- When you try loading data from tables, Hibernate doesn’t load all the mapped objects.
- After you reference a child object through getter methods, if the linked entity is not present in the session cache, then the proxy code will be entered to the database and load the linked object.
- It uses Java assist to effectively and dynamically generate sub-classed implementations of your entity objects.