Important Interview Questions on Hibernate

  • By Pooja Nandode-Bhavsar
  • April 26, 2024
  • JAVA Programming
Important Interview Questions on Hibernate

Important Interview Questions on Hibernate

Master Important Interview Questions on Hibernate! Unlock key insights into the Hibernate framework, mappings, transactions, and more. Elevate your skills with SevenMentor’s comprehensive training.

 

Q.1 Explain ORM?

ORM stands for Object Relational Mapping. It is a way to save or map objects in the database. It simplifies the creation of data, updation, and deletion of data. There are varieties of ORM tools available in the market like ibatis,TopLink, hibernate etc

 

Q.2 What is hibernate?

Hibernate is one of the most popular and widely used ORM tool to save Java’s objects into a database. But instead of writing any query we can directly save that object into the database without using any queries. Means we can directly convert objects into database using ORM(Object Relation(table) Mapping). Means mapping/conversion of objects into tables.

 

Q.3 Explain the core interfaces and classes of hibernate.

Configuration

configuration object is used to enable hibernate configuration in the app.It is the first Hibernate object that we  have to create in any Hibernate application. This is object is usually created only once during application start up. It allows the application to specify properties and mapping documents to be used. 

SessionFactory

The factory object holds information about Jdbc connection. And this factory object will be created using the interface called “SessionFactory”. We need to create a factory object one per database using a separate configuration file. if you are using multiple databases in our app, then you will have to create multiple SessionFactory objects.  SessionFactory object is heavy-weight

Session

Session is an Interface that binds the JDBC connection with a Java application. That means it creates a physical connection between the application and a database. The Session object is a lightweight object. Java’s objects are saved and retrieved through a Session object.In one factory we can create multiple sessions…and in one session we can perform multiple transactions

Query

Query is an inbuilt interface used to represent a hibernate query in an object-oriented format. As query is an interface, the object of it can be obtained using session.createQuery() method of session interface. It is used to execute HQL queries in the database

Criteria

The Hibernate Criteria Query Language is used to fetch the records of a table based on some criteria or constraints. Criteria API is nothing but set of methods that allows programmer  to fire complex type of query onto the database by applying different kinds of filtration rules and logical conditions. We can fetch complex type data from the database without applying queries

Transaction

A transaction is associated with a session object and usually created by calling session.beginTransaction() method. Basically, insert,update,and delete operations make changes into the database, which means this operation performs transactions into the database. Each transaction needs to be committed before executing that transaction into the database

 

Q.4 Is SessionFactory a thread-safe object?

Yes, sessioFactory is a thread-safe object means at time only one thread will get execute on the hibernate application.

 

For Free, Demo classes Call: 020-71173125

Registration Link: Click Here!

 

Q.5 Explain the difference between load() and get() methods.

The similarity between load() and get() method is both the methods are used to access/fetch an object from the database into our java application.Now lets understand the difference between these two.

 

get() load()
get() method of hibernate session returns null value If  given object is not found in cache as well as in database load() method throws ObjectNotFoundException if given object is not found in cache as well as in database
get() will  fire select query everytime whenever we are using that object or not  Load method does not fire a query everytime and can return a proxy in place and only initailize the object or fire the query , if any other getter method other than getId() is called on entity object
Use if u are not sure that object exists in db or not Use if you are sure that object exists

 

Q.6 Explain the difference between SQL and HQL?

 

HQL(methods) SQL(query)  
HQL stands for Hibernate Query language Structured Query Language
HQL is Database independent SQL is Database dependent(means if we are changing database,we need to change queries also)
Easy to learn for java programmer Easy to learn for DBA
from Student 

Here, Student is class/entity name

select * from Student

 Here, Student is table name

 

Q.7 How many types of mapping we can perform in hibernate-based applications?

 Basically, we  can perform 4 types of mapping in the hibernate app

 There can be 4 types of association mapping in hibernate.

One to One

One to Many

Many to One

Many to Many

1.One to One

In case of One to one mapping,  one record of one table should exactly mapped with one record of another table and In such case, a foreign key is created in the primary table. For an example, One question is having only one answer and one person is having only one adhar number

3.Many to One

In simple terms, many to one mapping means that many row in a table can be mapped to exactly one row in another table

For an example, Multiple answers can be associated with only one question and multiple bank accounts associated with one person

4.Many to many

In simple terms, one to many mapping means that many rows in a table can be mapped to multiple rows in another table. 

Example, Multiple employees can be worked on multiple projects and that each project is done  by multiple employees.

For Free, Demo classes Call: 020-71173125

Registration Link: Java Training in Pune!

 

Q.8 Explain the mechanism of caching in hibernate?

Caching is a mechanism to enhance the performance of an application

Cache is used to reduce the number of queries of database

First level cache associated with session object

First level cache is enabled by default and this type of cache is enabled for one session only…so if we have multiple sessions in one factory so we can use second level cache there If we are accessing one same record over  multiple number of times(say 50 times) then hibernate will not fire queries over 50 times it just get that object one time and stored in first level cache To enabled second level cache we have to add “hibernateehcache dependency in pom.xml file

 Second level cache associated with factory(session1,session2,session3….)

second level cache does not fire select query in multiple sessions for all sessions in a one factory,it will fire select query only once

                    

By default first level cache is enabled in hibernate. To enable Second Level Caching for Hibernate , add this property to your xml file:


<!–     Enabling second level cache in the project –>

    <property name=“cache.use_second_level_cache”>true</property>

<property  name=“cache.region.factory_class”>org.hibernate.cache.ehcache.internal.EhcacheRegionFactory</property>

 Also we need to use below annotations in our entity

 @Cacheable   //means that the entity and its state/properties must be cached in the

hibernate

@Cache(usage = CacheConcurrencyStrategy.READ_ONLY)  //to enabled second level   cache and to BOOK as an entity in that cache.

 

Do watch the video on Java: Click Here 

 Author:-

Pooja Nandode-Bhavsar

Call the Trainer and Book your free demo class for now!!!

© Copyright 2020 | Sevenmentor Pvt Ltd.

Submit Comment

Your email address will not be published. Required fields are marked *

*
*