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 is a free software, which is distributed under GNU Lesser General public license 2.1.
    It is categorized under the Object Relational Mapping (ORM), which features the mapping of Java classes to data tables and mapping from Java data types to SQL data types.
  • It is written in Java and is JVM (Java Virtual Machine) platform based.
  • Hibernate provides the data query and retrieval facilities.
  • It is one of the most widely used ORM tools for the Java applications.
  • Hibernate is highly considered to be used in the enterprise applications for database operations.

It represents a unit of work with the database and most of the RDBMS (Relational Database Management System) supports transaction functionality.
In Hibernate, transactions are managed by an underlying transaction manager and transaction from JDBC or JTA.
It is an optional object and the Hibernate Application do not use this interface, instead, they handle the transactions in their code.

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.

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.

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.

@Entity annotation is used to declare a class as an entity.

Example

@Entity
@Table(name="posts")
public class Post{
String title;
String description;
}

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 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.

Hibernate have 3 ways of inheritance mapping, They are

  • Table per hierarchy
  • Table per concrete class
  • Table per subclass

Criteria objects are used to create and execute object-oriented Queries to retrieve the objects.

It is used to get a physical connection with a database. A session object is designed to instantiate each time an interaction is required with the database, whereas the persistent objects are retrieved using a session object.
The session objects are not thread-safe and must be created and destroyed as per the requirement.

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.

Important interfaces of Hibernate framework are:

  • SessionFactory (org.hibernate.SessionFactory)
    It is an immutable thread safe cache of compiled mappings for a single database.
    We are supposed to initialize SessionFactory once and then we are allowed to cache and reuse it.
    The SessionFactory instance is used to return the session objects for database operations.
  • Session (org.hibernate.Session)
    It is a single threaded and short-lived object, which represents a conversation between the persistent store and the application.
    The session should be opened only when it is required and should be closed as soon as the user is done.
    The session object is the interface between hibernate framework and Java application code and it provides methods for the CRUD operations.
  • Transaction (org.hibernate.transaction)
    It is a single threaded and short-lived object used by the application, which specifies atomic units of work.
    The application is abstracted from the underlying JDBC or JTA transaction.

In hibernate, the Java classes whose instances and objects are stored in database classes are called persistent classes.