0% found this document useful (0 votes)
8 views

Interview_questions_on_hibernate

Java Program interview question

Uploaded by

sshinde1622
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

Interview_questions_on_hibernate

Java Program interview question

Uploaded by

sshinde1622
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Q.1 Explain ORM?

ORM stands for Object Relational Mapping. It is a way to save or mapped object in
the database. It simplifies 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 object
into 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 object 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

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
transactions 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 hibernate application.

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 load() method throws ObjectNotFoundException if


session returns null value given object is not found in cache as well as in
If given object is not database
found in cache as well as
in database

get() will fire select query Load method does not fire a query everytime and can
everytime whenever we return a proxy in place and only initailize the object or
are using that object or fire the query , if any other getter method other than
not getId() is called on entity object

Use if u are not sure that Use if you are sure that object exists
object exists in db or not

Q.6 Explain the difference between SQL and HQL?

HQL(methods) SQL(query)

HQL stands for Hibernate Structured Query Language


Query language

HQL is Database SQL is Database dependent(means if we are


independent changing database,we need to change queries also)
Easy to learn for java Easy to learn for DBA
programmer

from Student select * from Student

Here, Student is Here, Student is table name


class/entity name

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


application?
Basically, we can perform 4 types of mapping in 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.

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 "hibernate" ehcache 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.EhcacheR
egionFactory</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 a entity in that cache

You might also like