BEGINNER LEVEL QUESTIONS
ADVANCED LEVEL QUESTIONS
INTERMEDIATE LEVEL QUESTIONS
www.edureka.co
www.edureka.co
www.edureka.co
What is Hibernate?
Hibernate is one of the most popular Java frameworks that simplify the
development of Java application to interact with the database. It is an
Object-relational mapping (ORM) tool. Hibernate also provides a reference
implementation of Java API.
01
www.edureka.co
www.edureka.co
What are the major advantages of Hibernate Framework?
open-
sourced
and
lightweight
Performance
is fast
Helps to
generate
independent
queries
Provides
facilities to
create a table
Query
statistics and
database
status
02
www.edureka.co
www.edureka.co
What are the advantages of using Hibernate over JDBC?
Supports inheritance,
associations, and collections
Implicitly provides transaction
management
It wraps exceptions and throw
JDBCExceptions which are the
unchecked exceptions
HQL is more object-
oriented and closer to Java
Eliminates a lot of
boiler-plate code
03
www.edureka.co
www.edureka.co
04 What is an ORM tool?
A technique that maps the object that is stored in the database. An ORM tool helps in
simplifying data creation, manipulation, and access
www.edureka.co
www.edureka.co
05 Why use Hibernate Framework?
01
02
03
04
05 It overcomes the exception-handling
Strengthens the object level
Code portability is easily handled
Changing of the databases cost a lot
working on JDBC
Overcomes the database dependency
faced in the JDBC
www.edureka.co
www.edureka.co
06 What are the different functionalities supported by Hibernate?
Exception handling is
not mandatory
Supports cache
memory
It also has an Auto
Primary Key
Generation support
It supports auto DDL
operations
It uses HQL
It is an ORM tool
www.edureka.co
www.edureka.co
07 What are the technologies that are supported by Hibernate?
www.edureka.co
HQL is the acronym of
Hibernate Query Language. It
is an Object-Oriented Query
Language and is independent
of the database.
www.edureka.co
08 What is HQL?
HQL is the acronym of
Hibernate Query Language. It
is an Object-Oriented Query
Language and is independent
of the database.
www.edureka.co
www.edureka.co
09 How to achieve mapping in Hibernate?
Association mappings are one of the key features of Hibernate. It supports
the same associations as the relational database model.
Many-to-one
Name
Employee EmpID
Department
Many-to-many
One-to-one
Employee Has A Name
www.edureka.co
03
Transaction
02
Session
www.edureka.co
10 Name some of the important interfaces of Hibernate framework?
01
Session Factory
www.edureka.co
11 What is One-to-One association in Hibernate?
@OneToOne annotation
Entity Query
Model the system for the entity for which you want to navigate the relationship in
your query or domain model
www.edureka.co
12 What is One-to-Many association in Hibernate?
@OneToMany annotation
One object can be associated with multiple/different objects. Talking about the
mapping, the One-to-Many mapping is implemented using a Set Java collection
Query
Entity
Query
Query
Query
Query
www.edureka.co
13 What is Many-to-Many association in Hibernate?
@ManyToMany annotation
Entity Query
Many-to-Many mapping requires an entity attribute and a @ManyToMany
annotation. It can either be unidirectional and bidirectional.
Unidirectional
Entity Query
Bidirectional
www.edureka.co
14 How to integrate Hibernate and Spring?
Model class
Note: You don’t need to use Hibernate Transaction Management
Hibernate Core Spring-ORMEntity Manager
www.edureka.co
15 What do you mean by Hibernate Configuration File?
Contains database-specific configurations
Hibernate Configuration File are Dialect information, so
that hibernate knows the database type and mapping file
or class details.
www.edureka.co
Copyright © 2017, edureka and/or its affiliates. All rights reserved.
www.edureka.co
www.edureka.co
16
Mention some important annotations used for Hibernate
mapping?
01
02
03
04
05
06 javax.persistence.GeneratedValue
javax.persistence.Column
javax.persistence.Access javax.persistence.Table
javax.persistence.Entity
javax.persistence.EmbeddedId
www.edureka.co
17 What is Session in Hibernate and how to get it?
Java application layer Hibernate
Create
Read
Update
Delete
To get the Session, you can execute HQL queries, SQL native queries
using the Session object.
www.edureka.co
18 What is Hibernate SessionFactory?
SessionFactory is the factory class that is used to get the Session objects.
it is created during application startup and kept for later use. This
SessionFactory is a thread-safe object which is used by all the threads
of an application
www.edureka.co
19
What is the difference between openSession and
getCurrentSession?
returns the session bound to the context and
for this to work, you need to configure it in
Hibernate configuration file.
getCurrentSession() openSession()
Helps in opening a new session. You should
close this session object once you are done
with all the database operations.
www.edureka.co
20 How do you configure a Hibernate file?
0201
03 04
05 06
07
Identify which properties of
POJOs need to be continued
03
Identify the POJOs
Create a database schema
Add Hibernate Java
libraries
Create a Hibernate XML
configuration file
Annotate each of the POJOs
Create a Hibernate Configuration object
that refers to your XML configuration file
Build a Hibernate SessionFactory
object and retrieve08
www.edureka.co
21 What are the key components of a Hibernate configuration
object?
Database Connection Class Mapping setup
www.edureka.co
22 Discuss the Collections in Hibernate
01 02ArrayList Vector
Collections
Lists
Sets
Map
www.edureka.co
23 What are the collection types in Hibernate?
Sets
List
Array
Map
www.edureka.co
24 What is a Hibernate Template class?
Helper Classes
HibernateDaoSupport HibernateTemplate
www.edureka.co
25 What are the benefits of using Hibernate template?
Automated Session closing
ability
The interaction with the Hibernate
Session is simplified
Exception handling is automated
www.edureka.co
26 Which are the design patterns that are used in Hibernate
framework?
Domain Model Pattern Data Mapper Proxy Pattern Factory Pattern
www.edureka.co
27 Define Hibernate Validator Framework
Data validation is used in the presentation layer
with the use of Javascript and the server-side code
before processing
www.edureka.co
28 What is Dirty Checking in Hibernate?
Permits developers and users to avoid time-
consuming write actions
www.edureka.co
29 How can you share your views on mapping description
files?
Mapping description files are used by the Hibernate to configure
functions.
*.hbm extension, which facilitates the mapping between database
tables and Java class.
To use mapping description files or not this entirely depends on
business entities
www.edureka.co
30 What is meant by Light Object Mapping?
The syntax is hidden from the business logic using specific
design patterns
www.edureka.co
Copyright © 2017, edureka and/or its affiliates. All rights reserved.
www.edureka.co
www.edureka.co
31 What is meant by Hibernate tuning?
Optimizing the
performance of
Hibernate applications
is known as Hibernate
tuning
SQL Optimization
Session Management
Data Caching
www.edureka.co
32
What is Transaction Management in Hibernate? How does
it work?
Transaction
Management is a
process of managing a
set of commands or
statements
A transaction is associated with Session
and is instantiated by calling
session.beginTransaction().
www.edureka.co
33
How do you integrate Hibernate with Struts2 or Servlet
web applications?
Register a custom
ServletContextListener
Initialize the Hibernate
Session, store it in the
servlet context.
Action class helps
in getting the
Hibernate Session
www.edureka.co
34 What are the different states of a persistent entity?
Transient is not associated with the Session and has
no representation in the database
Using Persistent You can make a transient instance
persistent by associating it with a Session.
Using Detached If you close the Hibernate Session, the
persistent instance will become a detached instance.
www.edureka.co
35 How can the primary key be created by using Hibernate?
A Primary key is a special relational database
table column designated to uniquely identify all
table records. It is specified in the configuration
file hbm.xml.
www.edureka.co
36
Explain about Hibernate Proxy and how it helps in Lazy
loading?
Hibernate uses a
proxy object in order
to support Lazy
loading.
Hibernate doesn’t
load all the
mapped objects
After you reference
a child object
through getter
methods, then the
proxy code will be
entered to the
database
Uses Java assist to
effectively and dynamically
generate sub-classed
implementations
www.edureka.co
37 How can we see Hibernate generated SQL on console?
www.edureka.co
38 What is Query Cache in Hibernate?
Hibernate implements a separate cache
region for queries resultset that integrates
with the Hibernate second-level cache.
Note: This is only useful for queries that are run frequently with the same parameters.
www.edureka.co
39 What is the benefit of Native SQL query support in Hibernate?
Hibernate provides an
option to execute Native
SQL queries through the use
of the SQLQuery object
Native SQL Query comes handy
when you want to execute
database-specific queries that are
not supported by Hibernate API
such query hints or the Connect
keyword in Oracle Database.
www.edureka.co
40 What is Named SQL Query?
You can define a Named SQL
Query at a central location and
use them anywhere in the code.
@NamedQuery
@NamedNativeQuery
www.edureka.co
41 When do you use merge() and update() in Hibernate?
01
02
Update
If you are sure that the Hibernate
Session does not contain an already
persistent instance with the same id .
merge()
Helps in merging your modifications
at any time without considering the
state of the Session.
www.edureka.co
42 Difference between get() vs load() method in Hibernate?
It will throw an exception if an object with an
ID passed to them is not found.
load() get()
Will return null
Can return proxy without hitting the database
unless required
It always goes to the database
www.edureka.co
43 Difference between the first and second level cache in Hibernate?
The first-level cache is
maintained at Session level
The second level cache is
maintained at a SessionFactory
level and is shared by all
sessions.
www.edureka.co
44 Difference between Session and SessionFactory in Hibernate?
A Session is a single-
threaded, short-lived object.
It provides the first-level
cache.
Session
SessionFactory is immutable
and shared by all Session. It
also lives until the Hibernate
is running.
SessionFactory
www.edureka.co
45 Difference between save() and saveOrUpdate() method of
Hibernate?
save()
vs
saveOrUpdate()
save() can only Insert records but saveOrUpdate() can either
Insert or Update records.
www.edureka.co
46 Difference between sorted and ordered collection in Hibernate?
Sorted collection sort the data
in JVM’s heap memory using
Java’s collection framework
sorting methods
Ordered collection is sorted
using order by clause in the
database itself
www.edureka.co
47
Difference between the transient, persistent and detached state
in Hibernate?
New objects are created in
the Java program but are not
associated with any
Hibernate Session.
Transient state
An object which is associated
with a Hibernate session is called
Persistent object. You can call
save() or persist()
Persistent state
You can re-attach a detached
object to Hibernate sessions
by calling either update() or
saveOrUpdate() method
Detached state
www.edureka.co
Managed associations
48 Difference between managed associations and Hibernate
associations?
Hibernate Associations These associations are unidirectional
Relate to container management persistence and are
bi-directional
www.edureka.co
49
What are the best practices that Hibernate recommends for
persistent classes?
Java classes need a default
constructor
Classes should
contain an ID
Attributes should be
declared private
www.edureka.co
50 What are the best practices to follow with Hibernate framework?
www.edureka.co
Our
Strategy
Avoid Many-to-Many relationships, it can be
easily implemented using bidirectional One-
to-Many and Many-to-One relationships.
For collections, try to use Lists,
maps and sets. Avoid array
because you don’t get benefit of
lazy loading.
Prefer DAO pattern for exposing
the different methods that can be
used with entity bean
For web applications, always try
to use JNDI DataSource rather
than configuring to create a
connection in hibernate.
Always check the primary key field access
By default hibernate set the field
values directly, without using
setters
Use native sql query only when it
can’t be done using HQL
Use named queries wisely, keep it
at a single place for easy
debugging
Copyright © 2017, edureka and/or its affiliates. All rights reserved.
www.edureka.co
www.edureka.co

Hibernate Interview Questions | Edureka

  • 2.
    BEGINNER LEVEL QUESTIONS ADVANCEDLEVEL QUESTIONS INTERMEDIATE LEVEL QUESTIONS www.edureka.co
  • 3.
  • 4.
    www.edureka.co What is Hibernate? Hibernateis one of the most popular Java frameworks that simplify the development of Java application to interact with the database. It is an Object-relational mapping (ORM) tool. Hibernate also provides a reference implementation of Java API. 01 www.edureka.co
  • 5.
    www.edureka.co What are themajor advantages of Hibernate Framework? open- sourced and lightweight Performance is fast Helps to generate independent queries Provides facilities to create a table Query statistics and database status 02 www.edureka.co
  • 6.
    www.edureka.co What are theadvantages of using Hibernate over JDBC? Supports inheritance, associations, and collections Implicitly provides transaction management It wraps exceptions and throw JDBCExceptions which are the unchecked exceptions HQL is more object- oriented and closer to Java Eliminates a lot of boiler-plate code 03 www.edureka.co
  • 7.
    www.edureka.co 04 What isan ORM tool? A technique that maps the object that is stored in the database. An ORM tool helps in simplifying data creation, manipulation, and access www.edureka.co
  • 8.
    www.edureka.co 05 Why useHibernate Framework? 01 02 03 04 05 It overcomes the exception-handling Strengthens the object level Code portability is easily handled Changing of the databases cost a lot working on JDBC Overcomes the database dependency faced in the JDBC www.edureka.co
  • 9.
    www.edureka.co 06 What arethe different functionalities supported by Hibernate? Exception handling is not mandatory Supports cache memory It also has an Auto Primary Key Generation support It supports auto DDL operations It uses HQL It is an ORM tool www.edureka.co
  • 10.
    www.edureka.co 07 What arethe technologies that are supported by Hibernate? www.edureka.co
  • 11.
    HQL is theacronym of Hibernate Query Language. It is an Object-Oriented Query Language and is independent of the database. www.edureka.co 08 What is HQL? HQL is the acronym of Hibernate Query Language. It is an Object-Oriented Query Language and is independent of the database. www.edureka.co
  • 12.
    www.edureka.co 09 How toachieve mapping in Hibernate? Association mappings are one of the key features of Hibernate. It supports the same associations as the relational database model. Many-to-one Name Employee EmpID Department Many-to-many One-to-one Employee Has A Name www.edureka.co
  • 13.
    03 Transaction 02 Session www.edureka.co 10 Name someof the important interfaces of Hibernate framework? 01 Session Factory www.edureka.co
  • 14.
    11 What isOne-to-One association in Hibernate? @OneToOne annotation Entity Query Model the system for the entity for which you want to navigate the relationship in your query or domain model www.edureka.co
  • 15.
    12 What isOne-to-Many association in Hibernate? @OneToMany annotation One object can be associated with multiple/different objects. Talking about the mapping, the One-to-Many mapping is implemented using a Set Java collection Query Entity Query Query Query Query www.edureka.co
  • 16.
    13 What isMany-to-Many association in Hibernate? @ManyToMany annotation Entity Query Many-to-Many mapping requires an entity attribute and a @ManyToMany annotation. It can either be unidirectional and bidirectional. Unidirectional Entity Query Bidirectional www.edureka.co
  • 17.
    14 How tointegrate Hibernate and Spring? Model class Note: You don’t need to use Hibernate Transaction Management Hibernate Core Spring-ORMEntity Manager www.edureka.co
  • 18.
    15 What doyou mean by Hibernate Configuration File? Contains database-specific configurations Hibernate Configuration File are Dialect information, so that hibernate knows the database type and mapping file or class details. www.edureka.co
  • 19.
    Copyright © 2017,edureka and/or its affiliates. All rights reserved. www.edureka.co
  • 20.
  • 21.
    16 Mention some importantannotations used for Hibernate mapping? 01 02 03 04 05 06 javax.persistence.GeneratedValue javax.persistence.Column javax.persistence.Access javax.persistence.Table javax.persistence.Entity javax.persistence.EmbeddedId www.edureka.co
  • 22.
    17 What isSession in Hibernate and how to get it? Java application layer Hibernate Create Read Update Delete To get the Session, you can execute HQL queries, SQL native queries using the Session object. www.edureka.co
  • 23.
    18 What isHibernate SessionFactory? SessionFactory is the factory class that is used to get the Session objects. it is created during application startup and kept for later use. This SessionFactory is a thread-safe object which is used by all the threads of an application www.edureka.co
  • 24.
    19 What is thedifference between openSession and getCurrentSession? returns the session bound to the context and for this to work, you need to configure it in Hibernate configuration file. getCurrentSession() openSession() Helps in opening a new session. You should close this session object once you are done with all the database operations. www.edureka.co
  • 25.
    20 How doyou configure a Hibernate file? 0201 03 04 05 06 07 Identify which properties of POJOs need to be continued 03 Identify the POJOs Create a database schema Add Hibernate Java libraries Create a Hibernate XML configuration file Annotate each of the POJOs Create a Hibernate Configuration object that refers to your XML configuration file Build a Hibernate SessionFactory object and retrieve08 www.edureka.co
  • 26.
    21 What arethe key components of a Hibernate configuration object? Database Connection Class Mapping setup www.edureka.co
  • 27.
    22 Discuss theCollections in Hibernate 01 02ArrayList Vector Collections Lists Sets Map www.edureka.co
  • 28.
    23 What arethe collection types in Hibernate? Sets List Array Map www.edureka.co
  • 29.
    24 What isa Hibernate Template class? Helper Classes HibernateDaoSupport HibernateTemplate www.edureka.co
  • 30.
    25 What arethe benefits of using Hibernate template? Automated Session closing ability The interaction with the Hibernate Session is simplified Exception handling is automated www.edureka.co
  • 31.
    26 Which arethe design patterns that are used in Hibernate framework? Domain Model Pattern Data Mapper Proxy Pattern Factory Pattern www.edureka.co
  • 32.
    27 Define HibernateValidator Framework Data validation is used in the presentation layer with the use of Javascript and the server-side code before processing www.edureka.co
  • 33.
    28 What isDirty Checking in Hibernate? Permits developers and users to avoid time- consuming write actions www.edureka.co
  • 34.
    29 How canyou share your views on mapping description files? Mapping description files are used by the Hibernate to configure functions. *.hbm extension, which facilitates the mapping between database tables and Java class. To use mapping description files or not this entirely depends on business entities www.edureka.co
  • 35.
    30 What ismeant by Light Object Mapping? The syntax is hidden from the business logic using specific design patterns www.edureka.co
  • 36.
    Copyright © 2017,edureka and/or its affiliates. All rights reserved. www.edureka.co
  • 37.
  • 38.
    31 What ismeant by Hibernate tuning? Optimizing the performance of Hibernate applications is known as Hibernate tuning SQL Optimization Session Management Data Caching www.edureka.co
  • 39.
    32 What is TransactionManagement in Hibernate? How does it work? Transaction Management is a process of managing a set of commands or statements A transaction is associated with Session and is instantiated by calling session.beginTransaction(). www.edureka.co
  • 40.
    33 How do youintegrate Hibernate with Struts2 or Servlet web applications? Register a custom ServletContextListener Initialize the Hibernate Session, store it in the servlet context. Action class helps in getting the Hibernate Session www.edureka.co
  • 41.
    34 What arethe different states of a persistent entity? Transient is not associated with the Session and has no representation in the database Using Persistent You can make a transient instance persistent by associating it with a Session. Using Detached If you close the Hibernate Session, the persistent instance will become a detached instance. www.edureka.co
  • 42.
    35 How canthe primary key be created by using Hibernate? A Primary key is a special relational database table column designated to uniquely identify all table records. It is specified in the configuration file hbm.xml. www.edureka.co
  • 43.
    36 Explain about HibernateProxy and how it helps in Lazy loading? Hibernate uses a proxy object in order to support Lazy loading. Hibernate doesn’t load all the mapped objects After you reference a child object through getter methods, then the proxy code will be entered to the database Uses Java assist to effectively and dynamically generate sub-classed implementations www.edureka.co
  • 44.
    37 How canwe see Hibernate generated SQL on console? www.edureka.co
  • 45.
    38 What isQuery Cache in Hibernate? Hibernate implements a separate cache region for queries resultset that integrates with the Hibernate second-level cache. Note: This is only useful for queries that are run frequently with the same parameters. www.edureka.co
  • 46.
    39 What isthe benefit of Native SQL query support in Hibernate? Hibernate provides an option to execute Native SQL queries through the use of the SQLQuery object Native SQL Query comes handy when you want to execute database-specific queries that are not supported by Hibernate API such query hints or the Connect keyword in Oracle Database. www.edureka.co
  • 47.
    40 What isNamed SQL Query? You can define a Named SQL Query at a central location and use them anywhere in the code. @NamedQuery @NamedNativeQuery www.edureka.co
  • 48.
    41 When doyou use merge() and update() in Hibernate? 01 02 Update If you are sure that the Hibernate Session does not contain an already persistent instance with the same id . merge() Helps in merging your modifications at any time without considering the state of the Session. www.edureka.co
  • 49.
    42 Difference betweenget() vs load() method in Hibernate? It will throw an exception if an object with an ID passed to them is not found. load() get() Will return null Can return proxy without hitting the database unless required It always goes to the database www.edureka.co
  • 50.
    43 Difference betweenthe first and second level cache in Hibernate? The first-level cache is maintained at Session level The second level cache is maintained at a SessionFactory level and is shared by all sessions. www.edureka.co
  • 51.
    44 Difference betweenSession and SessionFactory in Hibernate? A Session is a single- threaded, short-lived object. It provides the first-level cache. Session SessionFactory is immutable and shared by all Session. It also lives until the Hibernate is running. SessionFactory www.edureka.co
  • 52.
    45 Difference betweensave() and saveOrUpdate() method of Hibernate? save() vs saveOrUpdate() save() can only Insert records but saveOrUpdate() can either Insert or Update records. www.edureka.co
  • 53.
    46 Difference betweensorted and ordered collection in Hibernate? Sorted collection sort the data in JVM’s heap memory using Java’s collection framework sorting methods Ordered collection is sorted using order by clause in the database itself www.edureka.co
  • 54.
    47 Difference between thetransient, persistent and detached state in Hibernate? New objects are created in the Java program but are not associated with any Hibernate Session. Transient state An object which is associated with a Hibernate session is called Persistent object. You can call save() or persist() Persistent state You can re-attach a detached object to Hibernate sessions by calling either update() or saveOrUpdate() method Detached state www.edureka.co
  • 55.
    Managed associations 48 Differencebetween managed associations and Hibernate associations? Hibernate Associations These associations are unidirectional Relate to container management persistence and are bi-directional www.edureka.co
  • 56.
    49 What are thebest practices that Hibernate recommends for persistent classes? Java classes need a default constructor Classes should contain an ID Attributes should be declared private www.edureka.co
  • 57.
    50 What arethe best practices to follow with Hibernate framework? www.edureka.co Our Strategy Avoid Many-to-Many relationships, it can be easily implemented using bidirectional One- to-Many and Many-to-One relationships. For collections, try to use Lists, maps and sets. Avoid array because you don’t get benefit of lazy loading. Prefer DAO pattern for exposing the different methods that can be used with entity bean For web applications, always try to use JNDI DataSource rather than configuring to create a connection in hibernate. Always check the primary key field access By default hibernate set the field values directly, without using setters Use native sql query only when it can’t be done using HQL Use named queries wisely, keep it at a single place for easy debugging
  • 58.
    Copyright © 2017,edureka and/or its affiliates. All rights reserved. www.edureka.co
  • 59.