Loading…
Hibernate Interview questions

An Overview of Hibernate

Hibernate or Hibernate ORM is a framework and a mapping tool for Java. It is an object-relational mapping tool that gives an object-oriented domain model to the relational database. The structure is able to handle the problems of object-relational impedance mismatch by the replacement of persistent and direct database access with the functions of high-level object handling functions. There are several Hibernate Interview Questions which must be learned by the students to give their best shot and get the desired job.

Development History

Hibernate ORM was developed by Gavin King in 2001 as a replacement to using EJB2-style entity beans. The latest version is Hibernate ORM 5.4.0, and it was released in December 2018. If you are looking for Hibernate Interview Questions, then this is the best platform for you.

Advantages

  • Database independent can work with DB2, SQL, MySQL, Oracle, etc.
  • Get the benefits of OOP Concepts such as encapsulation, inheritance, etc.
  • No requirement of hitting database for related queries, directly cache it and use it
  • Easy to write, support and read
  • Lazy loading is possible

Hibernate Interview questions and answers

Looking for a new job? Do not miss to read our Hibernate Interview questions and answers. Whether you are a fresher or an experienced, these questions and answers that can help you to crack your interview.

It is an ORM developed for mapping tool for Java application program. It has been used in the development of Java application program as the primary function of Hibernate ORM is mapping the classes to the database tables and the types of Java data to SQL data types.

Other uses are-
  • Retrieving the data results
  • Writing data queries
  • Solves the problems of the mismatch of the traditional object and relational impedance issue

Java API or known as JPA is an interface specification of Java application program. It is used to describes the relational data’s management in the Java-based applications along with Enterprise Edition and Java platform.

There are four transactions in Hibernate ORM, and these are Atomicity, Consistency, Isolation, and Durability. Acronym used is ACID. These are the properties which every transaction follows.

The procedure of synchronizing the persistence context state with the database is known as flushing. There are a set of methods sent by Hibernate Session and EntityManager with the help of which the persistence state of an entity can be changed by the application developer.

The evict method is used for detaching the object from hibernate provides evict () method and session cache. After this process, there is no persistence in the change of the object. The detachment of the associated objects can also take place with the mapping associated with the cascade=” evict.”

Hibernate SessionFactory is an interface, and it is created with the help of Configuration object. It consists of all the database related property details and in this, either hibernate.properties, the hibernate.cfg.xml file is used for pulling it. In the case of the increased number of multiple databases, then the requirement of creating one SessionFactory per database.

Session Factory is used to configure objects which is further responsible for allowing instantiation of the Session object and turning the configuration Hibernation for supplied configuration file application. Threads of an application use SessionFactory in Hibernation.

S.no Hibernate Save() SaveorUpdate method()
1. Work to generate a new identifier and then inserting the record into the database It either can UPDATE or INSERT because as per the existence of record.save() method can be failed with the persistence of the primary key
2. Used for bringing a transient object to a persisting state It can bring both new and existing, i.e., transient and detached into a persistent state or can be said that this method can be used for re-attaching a detached object into Session
Write the following queries-

1. Use CreateNativeSQL Method for calling a procedure

  • Query query = session.createSQLQuery (“CALL GetAllFoos ()”).addEntity;
  • List allFoos= query.list();

2. Use @NamedNativeQueries to call a stored procedure

3. Use @NamedStoreProcedureQuery to call a stored procedure

Example

                                                    

Use @NamedNativeQueries to call a stored procedure

@NamedNativeQueries({

          @NamedNativeQuery(

   name = "callGetAllFoos",

   query = "CALL GetAllFoos()",

      resultClass = Foo.class)

                         })

@Entity

public class Foo implements Serializable {

         // Model definition

}

Use @NamedStoreProcedureQuery to call a stored procedure

@NamedStoredProcedureQuery(

     name="GetAllFoos",

     procedureName="GetAllFoos",

     resultClasses = { Foo.class }

  )

@Entity

public class Foo implements Serializable {

 // Model Definition

}

S.no SessionFactory objects Session
1. There is one SessionFactory object per application There is one Session object per client
2. Used for creation and management and it is threaded safe Sessions Session is not threaded safe and give CRUD interface to the mapped classes
Hibernate use SQL databases for regularly testing by regularly handling in the Hibernate product lifecycle or Hibernate developers:
  • Oracle 11g, 11g RAC
  • MySQL 5.1, 5.5.
  • PostgreSQL 8.4, 9.1
  • DB2 9.7 or above.
  • Microsoft SQL Server 2008.
  • Sybase ASE 15.5 (jConnect 6.0)

Hibernate offers retrieval facilities and data query services along with generating SQL calls and relaxing the developer from handling the project manually and conversion of the object of the set of the result. These are the reasons which make Hibernate an object-relational model for the mapping tool for Java language.

Hibernate object states are as follows-
  • Detached Object State
  • Persistent Object State
  • Transient Object State
S.no Persistent Objects Transient Objects
1. In this state, the database has the saved objects The objects are not kept in the database in this state of Hibernation
2. Example- An entity from a repository comes under persistent objects Until persisted, the creation of a new entity is called transients

Transactions are required for protecting and maintaining the data and their consistency in Hibernate. Java application program framework, can modify several things in the databases, but transactions are needed for retaining the database and for this, there is an availability of Transaction Interface.

The default annotation for a property in the Java framework is a @ld annotation, where Hibernate assumes that the annotation is on the object’s access properties and detects that it is on the field. Placing the annotation @ld on getId() method gives access to the features through getter and setter methods and that too by default.

Configuration settings are required for the database and various other related parameters in Hibernate. The format for displaying such information is supplied as an XML file named hibernate.cffg.xml and hibernate.properties know as standard Java properties file.

The object or relational mappings are the files which are defined in an XML document which instruct Hibernate in mapping the defined classes or class to the tables in the database.

S.no JDBC Hibernate
1. Standard API ORM (Object-Relational Mapping)
2. Java applications used JDBC for communicating with the database Used for mapping Java objects to a corresponding relational database.

To read more Hibernate Interview Questions, and make the concepts clear, you can visit the Best Interview Question.

Ask Question