Hibernate Interview Questions

Hibernate Interview Questions

Hibernate is an ORM (object-relational mapping ) tool for Java programming language. It is developed by Red Hat and was started by Gavin King with his colleagues in 2001. The original aim was to offer better persistence capabilities than those offered by EJB2 (Enterprise Java Beans version 2), they wanted to simplify the complexities and supplement certain missing features.

Below we have provided hibernate Interview questions are specifically designed to give you the basic conceptual information at the beginning and later with the core nature of the subject. The information which has been provided will brush up your mind and will substantially contribute to impressing the interviewer so that you fly with high colors.

Download Hibernate Interview Questions PDF

Hibernate Interview Questions

Hibernate caches Query data and makes the application run faster.
If used correctly, the hibernate cache can be very useful in achieving the faster application running performance.
The main idea lying behind the cache is reducing the number of database queries, which results in reduced throughput time of the application.
HQL is the acronym of Hibernate Query Language.It considers the java objects in a similar way as that of the SQL.
It is an Object-Oriented Query Language and is independent of the database.
The concurrency strategies are the mediators who are responsible for storing items and retrieving them from the cache.In case of enabling a second level cache, the developer must decide for each persistent class and collection, and also which cache concurrency, has to be implemented.

Following are the concurrency strategies that can be implemented by the developer:

  • Transactional: This strategy is used mostly to read data where the prevention of stale data is critical in concurrent transactions, in the unique case of an update.
  • Read- Only: This strategy is compatible with the data that can’t be modified. We can use it for reference data only.
  • Read-Write: It is similar to transactional strategy. where we read mostly data and prevention of stale data is critical.
  • Non-strict-Read-Write: This strategy assures no guarantee of consistency between the database and cache. We can use this strategy only if the data can be modified and a small likelihood of stale data is not the critical concern.
The Key components of Hibernate are:
  • Session: It is used to get a physical network with a database.
  • Transaction: It represents the unit of work with a database.
  • Query: It uses SQL and HQL string to retrieve the data from the database and create objects.
  • Criteria: It is used create and execute object-oriented queries and retrieve the objects.
  • Configuration: It represents the properties of files required by Hibernate
  • Session Factory: It configures hibernate for the application using the provided configuration file and instantiates the session object.
It is similar to the many-to-one association and the difference lies in the column that will be set as a unique one.The many-to-one element is used to define one-to-one association.
To the defined variable a name attribute is set in the parent class and the column attribute is used to set column name in the parent table, which is unique so that only one object gets associated with another.
Criteria objects are used to create and execute object-oriented Queries to retrieve the objects.
Session.createQuery is used to create a new instance of a query for the HQL query string.
Session.createSQLQuery is used to create a new instance of a query for the SQL query string.
Hibernate configuration file:
It contains database specific configurations and is used to initialize SessionFactory.
It provides database credentials or JNDI resource information in the hibernate configuration XML file.
Dialect information is another important part of the hibernate configuration file.
Hibernate Mapping file:
It is used to define the database table column mappings and entity bean fields.
We use JPA annotations for mappings, but when we are using the third party classes sometimes XML mapping files becomes handy and we cannot use annotations.
It is a technique in where the objects are loaded on the requirement basis.
Since the Hibernate 3 version, the lazy loading is by default enabled so that the child objects are not loaded while the parent is loaded.
Some of the advantages of Hibernate are:
  • It provides Simple Querying of data.
  • An application server is not required to operate.
  • The complex associations of objects in the database can be manipulated.
  • Database access is minimized with smart fetching strategies.
  • It manages the mapping of Java classes to database tables without writing any code.
  • Properties of XML file is changed in case of any required change in the database.
In this association, one object can be associated with multiple objects.
The One-to-Many mapping is implemented using a Set Java collection that does not have any redundant element.
A One-to-Many element of the set element indicates the relation of one object to multiple objects.
It is an optional cache. And, always the first level cache will be consulted before any attempt is performed to locate an object in the second level cache. This cache can be configured on a pre-collection and per-class basis and it is mainly responsible for caching objects across the sessions.
It is session cache and mandatory cache.It is from first level cache through which all the requests must pass.
The session object stores an object under its control before committing it to the database.
In hibernate, a cache query can be implemented that results in sets and integrates closely with the second level cache.It is an optional feature and it requires two additional cache regions that can hold the cached query results and also the timestamps whenever a table is updated. This is useful only for the queries that run frequently holding the same parameters.