Upload
ravikumar-maddi
View
219
Download
0
Embed Size (px)
Citation preview
8/8/2019 Hibernate Interview Questins
1/9
1 Does Hibernate Session Object has any
cache associated with it by default ?
Yes, first-level caching is a mandatoryrequirement for Hibernate Session Object.
2 Is there any cache associated with
Hibernate SessionFactory Object?
Yes, there is an optional second-level cache
with Hibernate SessionFactory object.3 Can a single Hibernate Session object be
used across multiple threads running within a
process
No, Hibernate Session is basically single-
threaded and not to be used across multiplethreads.
4 Is this same for Hibernate SessionFactory
object as well?
No, Hibernate SessionFactory is basically
thread-safe, thus can be re-used acrossmultiple threads and multiple Transacions as
well.
8/8/2019 Hibernate Interview Questins
2/9
5 How can the second-level caching for
Hibernate SessionFactory object be disabled?
By setting appropriate hibernate.cacheconfiguration related properties, one can
enable/disable second-level caching for
Hibernate SessionFactory object
but only for once before SessionFactory is
created
6 Is it possible to use Hibernate Session
object with the scope and context defined by
JTA Transaction ?
Yes, starting with Hibernate 3.0.1 version,
Sessionfactory.getCurrentSession method has
the scope and context defined by the running
JTA Transaction scope and context. But as of
Hibernate 3.1 version, getCurrentSession
method of Hibernate
SessionFactory has the current Session Scope
and Context controlled by pluggable currentSession Context class, defined in
configuration parameter such as
hibernate.current_session_context_class.
8/8/2019 Hibernate Interview Questins
3/9
7 As of Hibernate 3.1 version can you be able
to explain how many ways scope and context
of Hibernate current contextual session be
handled?
As of Hibernate 3.1 version, Hibernate has
three ways to handle current contextual
session, such as
JTASessionContextThreadLocalSessionContext
ManagedSessionContext.
8 What is the difference between class tag
and component tag in Hibernate from the
persistence perspective?
class tag refers to an Entity that is persisted
with an identifier, while component tag means
the POJO associated with component tag is
persisted along with contained object as a
value type. So it doesn't require
an identifier, while the contained object has
an entity reference, not for the component
object.
8/8/2019 Hibernate Interview Questins
4/9
What is the difference between component
and dynamic component?
Component in Hibernate world, meanssomething that is embeded in
a contained object and there exist a
composition style of binding between
the contained object and the component
object. So component is declaredinside a class tag, just to say one type of use.
Dynamic-component has the same
characteristics as component but there exists
a difference in the way dynamic-component
can be used to map a bean's attribute, thiscan be of type java.util.Map with the key
defined in mapping file
and corresponding value from the table's
column.
So with dynamic-component, there can be
possibility of changing the attribute key/value
pair during deployment time, just by changing
8/8/2019 Hibernate Interview Questins
5/9
the name and column values in the mapping
configuration file.
9 Can a Hibernate Session, that isretrieved/created from a Hibernate
SessionFactory, be shared across multiple
concurrent Thread/Requests?
No, as Hibernate Session is not Thread-Safe,
so sharing it with multiple concurrent Thread
of execution or request will have concurrentcy
issues.
It is always better to use a single Hibernate
Session for a
single transaction, that means as long as the
transaction is running or not commited, a
single session can be used. As a Transaction
is running in isolation for each Thread or
request.
10 What is a good approach of handling
Hibernate Session, in case of any databaseexception or Hibernate Exception that is
occuring while a Hibernate Session is in use?
8/8/2019 Hibernate Interview Questins
6/9
As Hibernate Session caches Persistent
Object, in case of any Database exception or
Hibernate Exception occuring, then Session
cache might have some stale data or
Persistent Object not in sync with Database.
So it is always better to rollback Transaction,
close Session, in this circumtances.
11 How Hibernate manages Transaction
timeout in Managed and Non-managed
environments?
In Managed environment, that is JTA
transaction can handle timeout, so Hibernate
delegated Transaction timeout task to JTA
Transaction.
But in non-managed environment, Developer
has to explicitly specify Transaction timeout
parameter while using Transaction from
Hibernate Session by using setTimeOut(time
in seconds).But this feature cannot be fully compared
with JTA based Transaction timeout operation.
8/8/2019 Hibernate Interview Questins
7/9
Any one wants to comment on these
questions and answer, please do so.
12 What is the first level cache and thesecond level cache in Hibernate?
First level cache is associated with Hibernate
Session object, and is mandatory, while
second level cache is optionally associated
with Hibernate SessionFactory instance.
13 Which cache is cluster-level, first level or
second level?
Second level cache is optional, but can be
cluster-level.
How many different instance states exist foran instance of a persistent class while using
Hibernate?
There are three different instance states, such
as Transient, Persistent, and detached.
14 What are the instances having persistententity?
Persistent and detached instances are having
persistent entity. Can you please explain
8/8/2019 Hibernate Interview Questins
8/9
these three instance states with help of an
example?
Suppose in a web application, if a userinformation is passed from the registration
page shwon on browser for the first time, then
the UI helper class will be creating an instance
of the User class, and populate all the
relevant details onto the USER object. At this
level USER object has a Java identity but nopersistent identity. So this
USER object is transient instance state. Then
if this USER instance is sent to the business
level, and with help of DAO, is stored in
backend db, then this USER instance has gotboth Java and Persistent identities, so is
having Persistent instance state.
If this USER object is again sent to the UI level
for displaying an edit screen, then this USER
instance in UI tier is detached instance state,
as is having both Java and persistent identity.
But Hibernate cann't relate Java and
persistent identities for detached instance
states.
8/8/2019 Hibernate Interview Questins
9/9