Hibernate Interview Questions

Hibernate Interview Questions Download Hibernate Interview Questions PDF

Hibernate Interview Questions

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


public class Post{
String title;
String description;
These objects use SQL and HQL string to retrieve data from the database and create objects.
An instance of Query is used to bind query parameters, restrict the number of results returned by the query and finally to execute the query.
The Many-to-Many element indicates the relation between one object to many other objects and column attribute is used to link intermediate columns. A Many-to-Many mapping is implemented using a Set Java collection that does not have any redundant element.
This association is the common type of association where one object can be associated with multiple objects.
And Many-to-one element defines the Many-to-One association.To the defined variable, a name attribute is set in the parent class and column attribute sets the column name in the parent table.
In hibernate, the Java classes whose instances and objects are stored in database classes are called persistent classes.
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.
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 allows business code access the objects rather than Database tables.
  • It hides the details of SQL queries from OO logic.
  • It is based on JDBC “under hood”.
  • Dealing with database implementation is not required.
  • Entities are based on business concepts rather than database structures.
  • It generates the automatic key and Transaction management.
  • Application development is faster.

Hibernate have 3 ways of inheritance mapping, They are

  • Table per hierarchy
  • Table per concrete class
  • Table per subclass
Hibernate supports all the major RDMS.Following are the list of database engines supported by Hibernate:
  • HSQL Database Engine
  • DB2/NT
  • Oracle
  • Microsoft SQL Server Database
  • Sybase SQL Server
  • Informix Dynamic Server
  • MySQL
  • PostgreSQL
  • FrontBase

Difference between save and persist in Hibernate

Return TypeReturns Serializable objectReturns Void
  • It is used to define specific mappings from Java classes to database tables.
  • It is used to define the mapping of unique ID attribute in class to the primary key of the database table.
  • It is used to generate the primary key values automatically.
  • It is used to map a Java class property to a column in the database table.
  • It is used to map a java.util.set, java.util.Sortedset property in hibernate.
  • It is used to map a java.util.List property in hibernate.
  • It is used to map a java.util.Collection property in hibernate.
  • It is used to map a java.util.Map property in hibernate.
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.

Please Provide an Answer