Hibernate Interview Questions and Answers
Question - 51 : - What do you mean by Hibernate Configuration File?
Answer - 51 : -
Hibernate Configuration File mainly contains database-specific configurations and are used to initialize SessionFactory. Some important parts of the Hibernate Configuration File are Dialect information, so that hibernate knows the database type and mapping file or class details.
Question - 52 : - Mention some important annotations used for Hibernate mapping?
Answer - 52 : -
Hibernate supports JPA annotations. Some of the major annotations are:
- javax.persistence.Entity: This is used with model classes to specify they are entity beans.
- javax.persistence.Table: It is used with entity beans to define the corresponding table name in the database.
- javax.persistence.Access: Used to define the access type, field or property. The default value is field and if you want Hibernate to use the getter/setter methods then you need to set it to a property.
- javax.persistence.Id: Defines the primary key in the entity bean.
- javax.persistence.EmbeddedId: It defines a composite primary key in the entity bean.
- javax.persistence.Column: Helps in defining the column name in the database table.
- javax.persistence.GeneratedValue: It defines the strategy to be used for the generation of the primary key. It is also used in conjunction with javax.persistence.GenerationType enum.
Question - 53 : - What is Session in Hibernate and how to get it?
Answer - 53 : -
Hibernate Session is the interface between Java application layer and Hibernate. It is used to get a physical connection with the database. The Session object created is lightweight and designed to be instantiated each time an interaction is needed with the database. This Session provides methods to create, read, update and delete operations for a constant object. To get the Session, you can execute HQL queries, SQL native queries using the Session object.
Question - 54 : - What is Hibernate SessionFactory?
Answer - 54 : -
SessionFactory is the factory class that is used to get the Session objects. The SessionFactory is a heavyweight object so usually, it is created during application startup and kept for later use. This SessionFactory is a thread-safe object which is used by all the threads of an application. If you are using multiple databases then you would have to create multiple SessionFactory objects.
Question - 55 : - What is the difference between openSession and getCurrentSession?
Answer - 55 : -
This getCurrentSession() method returns the session bound to the context and for this to work, you need to configure it in Hibernate configuration file. Since this session object belongs to the context of Hibernate, it is okay if you don’t close it. Once the SessionFactory is closed, this session object gets closed.
openSession() method helps in opening a new session. You should close this session object once you are done with all the database operations. And also, you should open a new session for each request in a multi-threaded environment.
Question - 56 : - What is meant by Light Object Mapping?
Answer - 56 : -
The means that the syntax is hidden from the business logic using specific design patterns. This is one of the valuable levels of ORM quality and this Light Object Mapping approach can be successful in case of applications where there are very fewer entities, or for applications having data models that are metadata-driven.
Question - 57 : - What is Dirty Checking in Hibernate?
Answer - 57 : -
Hibernate incorporates Dirty Checking feature that permits developers and users to avoid time-consuming write actions. This Dirty Checking feature changes or updates fields that need to be changed or updated, while keeping the remaining fields untouched and unchanged.
Question - 58 : - Define Hibernate Validator Framework
Answer - 58 : -
Data validation is considered as an integral part of any application. Also, data validation is used in the presentation layer with the use of Javascript and the server-side code before processing. It occurs before persisting it in order to make sure it follows the correct format. Validation is a cross-cutting task, so we should try to keep it apart from the business logic. This Hibernate Validator provides the reference implementation of bean validation specs.
Question - 59 : - Which are the design patterns that are used in Hibernate framework?
Answer - 59 : -
There are a few design patterns used in Hibernate Framework, namely:
- Domain Model Pattern: An object model of the domain that incorporates both behavior as well as data.
- Data Mapper: A layer of the map that moves data between objects and a database while keeping it independent of each other and the map itself.
- Proxy Pattern: It is used for lazy loading.
- Factory Pattern: Used in SessionFactory.
Question - 60 : - What are the benefits of using Hibernate template?
Answer - 60 : -
The following are the benefits of using this Hibernate template class:
- Automated Session closing ability.
- The interaction with the Hibernate Session is simplified.
- Exception handling is automated.