Hibernate Interview Questions

Hibernate Interview Questions Download Hibernate Interview Questions PDF

Hibernate Interview Questions

Please Provide an Answer

Difference between save and persist in Hibernate

DifferenceSavePersist
Return TypeReturns Serializable objectReturns Void
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.
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.
  • 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.
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.
Few important benefits of Hibernate framework are:
  • Hibernates allows us to focus on business logic, eliminating all the boiler-plate code that comes with JDBC and handles the resources.
  • Code implementation becomes independent as Hibernate framework provides the support for XML and also to the JPA annotations.
  • HQL is powerful Query Language which is similar to SQL, and HQL understands the concepts of polymorphism, inheritance, and association, which makes it fully object-oriented.
  • Better performance can be achieved by Hibernate cache.
  • It supports Lazy initialization with the use of proxy objects and when required performs actual database queries.
  • We can execute native SQL queries using hibernate for vendor specific feature.

On the whole, hibernate makes it a better choice in the current market for ORM tool, as it contains all the features that you will require in an ORM tool.

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