Hibernate Interview Questions
Hibernate Interview Questions
1.What is ORM ?
4.What is Hibernate?
Hibernate is a pure Java object-relational mapping (ORM) and
persistence framework that allows you to map plain old Java objects to
relational database tables using (XML) configuration files.Its purpose is
to relieve the developer from a significant amount of relational data
persistence-related programming tasks.
• Improved productivity
o High-level object-oriented API
o Less Java code to write
o No SQL to write
• Improved performance
o Sophisticated caching
o Lazy loading
o Eager loading
• Improved maintainability
o A lot less code to write
• Improved portability
o ORM framework generates database-specific SQL for you
• Programmatic configuration
• XML configuration (hibernate.cfg.xml)
• Session interface
• SessionFactory interface
• Configuration interface
• Transaction interface
• Query and Criteria interfaces
Example :
<hibernate-mapping>
<class name="com.test.User" table="user">
<property column="USER_NAME" length="255"
name="userName" not-null="true" type="java.lang.String"/>
<property column="USER_PASSWORD" length="255"
name="userPassword" not-null="true" type="java.lang.String"/>
</class>
</hibernate-mapping>
load() get()
If you are not sure that the
Only use the load() method if you
object exists, then use one of the
are sure that the object exists.
get() methods.
load() method will throw an get() method will return null if
exception if the unique id is not the unique id is not found in the
found in the database. database.
load() just returns a proxy by
get() will hit the database
default and database won’t be hit
immediately.
until the proxy is first invoked.
Use update() if you are sure that the session does not contain an
already persistent instance with the same identifier, and merge() if
you want to merge your modifications at any time without
consideration of the state of the session.
23.Define HibernateTemplate?
org.springframework.orm.hibernate.HibernateTemplate is a helper
class which provides different methods for querying/retrieving data
from the database. It also converts checked HibernateExceptions into
unchecked DataAccessExceptions.
Example:
JDBC Hibernate
Hibernate is flexible and powerful
With JDBC, developer has to write
ORM solution to map Java classes
code to map an object model's
to database tables. Hibernate itself
data representation to a relational
takes care of this mapping using
data model and its corresponding
XML files so developer does not
database schema.
need to write code for this.
With JDBC, the automatic mapping Hibernate provides transparent
of Java objects with database persistence and developer does
tables and vice versa conversion is not need to write code explicitly to
to be taken care of by the map database tables tuples to
developer manually with lines of application objects during
code. interaction with RDBMS.
Hibernate provides a powerful
query language Hibernate Query
Language (independent from type
JDBC supports only native
of database) that is expressed in a
Structured Query Language (SQL).
familiar SQL like syntax and
Developer has to find out the
includes full support for
efficient way to access database,
polymorphic queries. Hibernate
i.e. to select effective query from
also supports native SQL
a number of queries to perform
statements. It also selects an
same task.
effective way to perform a
database manipulation task for an
application.
Application using JDBC to handle Hibernate provides this mapping
persistent data (database tables) itself. The actual mapping between
having database specific code in tables and application objects is
large amount. The code written to done in XML files. If there is
map table data to application change in Database or in any table
objects and vice versa is actually
to map table fields to object
properties. As table changed or
database changed then it’s then the only need to change XML
essential to change object file properties.
structure as well as to change
code written to map table-to-
object/object-to-table.
Hibernate reduces lines of code by
With JDBC, it is developer’s
maintaining object-table mapping
responsibility to handle JDBC
itself and returns result to
result set and convert it to Java
application in form of Java objects.
objects through code to use this
It relieves programmer from
persistent data in application. So
manual handling of persistent
with JDBC, mapping between Java
data, hence reducing the
objects and database tables is
development time and
done manually.
maintenance cost.
Hibernate, with Transparent
Persistence, cache is set to
application work space. Relational
tuples are moved to this cache as
a result of query. It improves
With JDBC, caching is maintained performance if client application
by hand-coding. reads same data many times for
same write. Automatic
Transparent Persistence allows the
developer to concentrate more on
business logic rather than this
application code.
In JDBC there is no check that Hibernate enables developer to
always every user has updated define version type field to
data. This check has to be added application, due to this defined
by the developer. field Hibernate updates version
field of database table every time
relational tuple is updated in form
of Java class object to that table.
So if two users retrieve same tuple
and then modify it and one user
save this modified tuple to
database, version is automatically
updated for this tuple by
Hibernate. When other user tries
to save updated tuple to database
then it does not allow saving it
because this user does not have
updated data.
• Bag
• Set
• List
• Array
• Map