Tech I Enjoy Logo

Custom Search




  Home >> Hibernate

Dated: 15-August-2011

Question :
If you want to react programmatically while executing methods of Hibernate session
instance, what are various ways to accomplish this while using Hibernate?

Answer :
By using Hibernate Event system and/or Hibernate Interceptors provided by 
Hibernate API, one can provide customized handlers and listeners for reacting
programmatically to the execution of various methods from Hibernate session
instance.

Question :
What will happen if there is a instance level variable defined within event
listener code?

Answer : As instance of Listener class file are shared across multiple requests, so it is not alright to use instance level variable within Listener class file, as far as thread-safe behavior is concerned, and unless the variable value is to be used in multiple request perspective.
Hibernate Interview Questions 1:
Does Hibernate Session Object has any cache associated with it by default ?
Hibernate Interview Answer 1:
Yes, first-level caching is a mandatory requirement for Hibernate Session Object.
Hibernate Interview Questions 2:
Is there any cache associated with Hibernate SessionFactory Object?
Hibernate Interview Answer 2:
Yes, there is an optional second-level cache with Hibernate SessionFactory
object.
Hibernate Interview Questions 3:
Can a single Hibernate Session object be used across multiple threads 
running within a process
Hibernate Interview Answer 3:
No, Hibernate Session is basically single-threaded and not to be used across
multiple threads.
Hibernate Interview Questions 4:
Is this same for Hibernate SessionFactory object as well?
Hibernate Interview Answer 4:
No, Hibernate SessionFactory is basically thread-safe, thus can be re-used 
across multiple threads and multiple Transacions as well.
Hibernate Interview Questions 5:
How can the second-level caching for Hibernate SessionFactory object be
disabled?


Hibernate Interview Answer 5:
By setting appropriate hibernate.cache configuration related properties, one
can enable/disable second-level caching for Hibernate SessionFactory object
but only for once before SessionFactory is created
Hibernate Interview Questions 6:
Is it possible to use Hibernate Session object with the scope and context
defined by JTA Transaction ?
Hibernate Interview Answer 6:
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
current Session Context class, defined in configuration parameter such as 
hibernate.current_session_context_class.
Hibernate Interview Questions 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?
Hibernate Interview Answer 7:
As of Hibernate 3.1 version, Hibernate has three ways to handle current contextual
session, such as
JTASessionContext
ThreadLocalSessionContext
ManagedSessionContext.
Hibernate Interview Questions 1 :
What is the difference between class tag and component tag in Hibernate
from the persistence perspective?

Hibernate Interview answer 1 :
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.

Hibernate Interview Questions 2 :
What is the difference between component and dynamic component?

Hibernate Interview answer 2 :
Component in Hibernate world, means something 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 declared
inside 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, 
this can 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 the name and 
column values in the mapping configuration file.


Hibernate Interview Question :
What are the different types of Modes are available, those can be
used along with Hibernate Session?

Hibernate Interview Answer :
Various Modes like CacheMode, LockMode, EntityMode, FlushMode, 
ScrollMode, these modes can be used along with Hibernate Session.

Hibernate Interview Question :
What are the various CacheMode available in Hibernate Version 3.2?

Hibernate Interview Answer :
Various CacheMode like GET, IGNORE, NORMAL, PUT, REFRESH are
available with Hibernate's second-level cache.

Hibernate Interview Question :
Is there any way not to use Hibernate's second-level cache, while
using operations of Hibernate Session?

Hibernate Interview Answer:
By setting CacheMode.IGNORE as the cache mode for any Hibernate
Session instance, before any operation on that session is carried
out. This way one can ignore Hibernate's second-level cache while
using operations of Session.

Hibernate Interview Question :
How to disable Hibernate's second-level cache from usage?

Hibernate Interview Answer:
Just by providing cache provider as org.hibernate.cache.NoCacheProvider
, one can disable use of Hibernate's second level cache.
Another way is by setting use_second_level_cache from hibernate.cache
property, as false.
Another way is to use CacheMode.IGNORE along with Hibernate's session.

Hibernate Interview Question :
What are the various steps to use Hibernate's second-level cache

Hibernate Interview Answer:
One has to define the supporting cache provider for any second-level
cache framework to be used, in Hibernate configuration file along with
the configuration for Hibernate's SessionFactory. 
Then it is required to enable the use_second_level_cache property
as true or providing appropriate cache mapping at class or collection
mapping related configuration.

Hibernate Interview Question :
What are the various types of cache providers support available with
Hibernate's second-level cache features in api?

Hibernate Interview Answer:
Various cache providers like EhCacheProvider, HashtableCacheProvider,
JndiBoundTreeCacheProvider, OptimisticTreeCacheProvider, OSCacheProvider 
, SwarmCacheProvider and TreeCacheProvider etc.

Hibernate Interview Question :
  
  If the project requirement to have the second level cache used in transactional
  context, which cache would you choose out of those Cache Providers?

Answer:

  JBoss TreeCache cache provider and cache framework can be a choice,
  as it can be used in clustered environment with ip multicast replication
  mode. And this cache can be used along with a transactional context.

Hibernate Interview Question :
  
  How about EHCache and OSCache providers from Hibernate version 3.0,
  can these be used in clustered environment, as of this version?

Answer:

  No, these cache providers are capable of running in-memory and disk
  modes, with no cluster way of execution.

Hibernate Interview Question :
  How can you avoid synchronization of persistent objects with the
  database, and do not want to retain this object in the first-level
  cache, when flush method is called on session?

Answer:
  
  By using evict method from session, one can remove specific object
  from first-level cache of session, and thus can avoid automatic
  synchronization of object with database, when flush method is called
  on session instance.

Hibernate Interview Question :
  Can you be able to evict all objects from the session cache? If yes, How?

Answer:
  Yes, it is possible to evict all objects from the session cache by using
  clear method on session instance.

Hibernate Interview Question :
  If anyone wants to perform similar activities with Hibernate's second-level
  cache, is it possible? If yes, how?

Answer:

  Yes, evict object(s) with or without criteria can be possible on
  Hibernate's second-level cache by using methods on Hibernate's
  SessionFactory, and methods are evict, evictCollection  and many
  more arguments available.

  
Hibernate Question :
 What are the different Transaction Factories available with Hibernate?
Hibernate Answer :
There are three different types of Transaction Factoryavailable with 
Hibenate 3.2 as JDBCTransactionFactory, JTATransactionFactory and 
CMTTransactionFactory.

Hibernate Question :
Which one is the default    transaction factory in    Hibernate 3.2?


Hibernate interview answer
JDBCTransactionFactory   is the default local
 transaction factory withHibernate 3.2.
Hibernate interview question
Can Hibernate Session Factory be bound to JNDI?

Hibernate interview answer
Yes, by configuring in hibernate.cfg file, session
factory can be bound to initial context (as defined by
properties hibernate.jndi.url and hibernate.jndi.class).

Hibernate interview question
Can Hibernate be used to call
stored procedures and SQL
statements?

Hibernate interview answer
Yes, there are provision in Hibernate 3.2, for defining
callable statements and SQL in mapping HBM files.

Hibernate interview question
Can the custom SQL be defined for creation of Java entity
object by loading values from database tables and
populating Java Object?

Hibernate interview answer
Yes, Javaentity objects can be loaded with custom SQL
queries and can be defined in HBM file in form of
HQL (Hibernate Query Language).

Hibernate interview question
What are the different Fetching Strategies available
with Hibernate 3.2?

Hibername interview answer
There are four different Fetching standards available in
Hibernate3.2, as follows: join fetching, select fetching,
batch fetching, sub-select fetching.

Hibernate interview question
What are the different types of statistics available in
Hibernate 3.2?

Hibernate interview answer
Different types of statistics like QueryStatistics,
CategorizedStatistics, CollectionStatistics, EntityStatistics
etc., available in Hibernate 3.2.

Hibernate interview question
How can you get a handle on Hibernate Statistics?

Hibernate interview answer
If Hibernate is deployed in a JMX enabled Application
server, then Hibernate provided a statistics service,
that can be registered as MBean with JMX server and be
used to retrieve different types of statistics available.
Hibernate statistics can be obtained from session
factory as well.

Hibernate interview question
Can Hibernate be used to map persistent entity POJO to
XML files?

Hibernate interview answer
Yes, Hibernate can be used to mapp XML file/tags to
POJO entity classes.

 Hibernate Question : If there are
 multiple databases to be used to interact with domain
 classes, how can session factory be able to manage
 multiple datasources?

Hibernate Answer :
 Each datasource will be configured to each session
 factory, and to use a single database, a session is
 created to use database.
 
Question : What is lazy initialization in Hibernate?

Answer :
When there is an association of one-to-one, or
one-to-many, or many-to-many between classes,
and on creation of one object, it has to be
decided whether to bring associated objects along
with this object or not. By setting lazy="true"
we instruct Hibernate not to bring the associated
object/objects during creation of the required object.
By setting lazy="false", it is the reverse, this means
we instruct Hibernate to bring all the associated
objects also at the time of returning the associating
object.

Hibernate interview Question : if there any impact on performance
by this attribute lazy ?

Hibernate interview Answer :
This is purely a configuration time decision one has
to take to use lazy attribute and its value (true/false)
appropriately. As SessionFactory is created once and reused,
all the configuration setting in HBM file is read once,
and cann't be changed at runtime.

Hibernate Question : What are the different states
of an instance in Hibernate?

Hibernate Answer :
There are three states that exist for any instance of a
class. These are transient, persistent and detached.
Those instances that are created but not associated with
any session or not saved in database are trasient objects.
Those instances that are created and be used in any of the
methods like save, saveOrUpdate, update of Session are
called persistent objects.
Those instances that were used in Session methods like save,
saveOrUpdate or update to be inserted or updated in database
table, and then session is flushed and closed, now these
objects are in JVM, but these are not bound to any session.


Hibernate interview question
How can certain type of logic executed on execution of
CRUD operation of session, without duplicating it across
many places in code base?

Hibernate interview answer
Hibernate Interceptors can be used to receive callback
for certain type of events or operations like save, delete,
load, update of session. Session Factory level interceptor
and session level interceptor. These Interceptors can be
used to have code for certain type of logic to be called
for every lifecycle method of session.

Hibernate interview question
How can multiple threads access session factory
simulteneously to create session instance?

Hibernate interview answer
session factory is thread-safe, so it is okay to be used
by many threads to have session from session factory,
but I think session is not thread safe and it should be
used by one thread at a time, and after use,
session has to be flushed and closed.

Hibernate interview question
How many ways Hibernate manages concurrency ?

Hibernate interview answer
Hibernate has different ways of managing concurrency.
These are  automatic versioning, detached object and
extended user sessions.

Hibernate interview question
What is the difference between uni-directional and
bi-directional associations?

Hibernate interview answer
uni-directional association allows object creation from
one direction only. Bi-directional association allows
object querying from both directions of fetching object
instances.

A->B, now querying A, can provide information on B as
well, based on lazy parameter, but in case of A<->B,
querying either A or B, will have value of B or A as
well, respectively.

Hibernate interview Question
What are the different contextual session in Hibernate?

Hibernate interview answer
There are three different types of contextual session Hibernate
provides, these are JTA session context, local thread session
context and managed session context. JTA session context is
applicable in case Hibernate session is running in JTA (Java
Transaction API), request thread level session scoped applicable
in case of local thread session, and managed session, requires
application to open, close and flush session, so creation of
session should be handled by application only.

Hibernate interview Question
Can you tell us difference between Hibernate HQL over SQL?

Hibernate interview answer
HQL is fully object oriented, with support for object
inheritence, polymorphism and association, but SQL
is more of Relational with structured form of queries.

Hibernate interview Question
What are the different scopes one can introduce while using
Interceptors with Hibernate?

Hibernate interview Answer
Probably, one can use interceptors with hibernate Session
scoped or SessionFactory scoped contexts, while using
Interceptors with Hibernate.

Hibernate interview Question
How many ways client application that uses Hibernate to
react to certain events?

Hibernate interview Answer
Probably, if I am not wrong, two ways one can react/act
to certain events generated out of Hibernate Framework.
These are either Interceptors or event systems.

Hibernate interview Question
Can I be able to persist a XML DOM object tree to database
by defining mapping between XML DOM to database table,
without using POJOs?

Hibernate interview Answer
Yes, one can use Hibernate mapping to persist XML DOM tree
hierarchy to database tables.


Hibernate Interview Question :
Suppose Hibernate Filters are defined in HBM file for a class,
but need is to not use this filter at runtime, Is it possible?

Hibernate Interview Answer :

Hibernate Filters are to be enabled for any instance of Hibernate
session before use. So whenever is it not required, those filters
won't be used.

Hibernate Interview Question :
How can the Hibernate Filter be enabled/ disabled for a session?

Hibernate Interview answer :
session.enableFilter(method parameters/arguments) is the method for
enabling/disabling filter for Hibernate Session instance.

Hibernate Interview Question : 
In case of a requirement as to use combination of fields from different
class files those are mapped to different tables. Or in short the requirement
is to have functionality of a view (database perspective) but not create a 
view in database.

Hibernate Interview answer :
Yes, using Hibernate Filters one can define certain filter conditions in
different class file mapping so as to filter the final query result as per 
the mapping and filter definition.

Hibernate Interview Question :
What are the various persistent objects fetching strategies defined in 
Hibernate3 ?

Hibernate Interview Answer :

There are four different types of persistent objects fetching strategies
defined in Hibernate3, such as Joing fetching, select fetching, Sub-select 
fetching and Batch fetching strategies.

Hibernate Interview Question :
Can these fetching strategies for retrieving persistent objects, those are
defined in Object Relational Mapping in configuration, be able to over-ridden ?

Hibernate Interview answer :

Yes, fetching strategies as defined in Mapping configuration files can be
over-ridden by using HQL or Criteria defined/used with Hibernate Session
instance.

Hibernate Interview Question :
Can the property tag definition of the class tag for the POJO class that is being
used in O/R Mapping, be lazily loaded by using lazy="true"?

Hibernate Interview Answer :
Yes, we can define lazy="true" for any property within a class tag from the 
O/R mapping file. But we must have to apply proper instrumentation of the build-time
bytecode of the class that is being used, or else this lazy definition will be ignored
while fetching respective persistent object.

Hibernate Interview Question :
While working with large binary stream or serializable object to be used with database
using Hibernate Session, is there any setting that is to be used in Hibernate specific
configuration file?

Hibernate Interview Answer :
Yes, hibernate.jdbc.use_streams_for_binary setting can be used with value true or false,
in case you want to use large binary or serializable data to/from database.

Hibernate Interview Question :
While using outer join fetch strategy, can you impose certain depth or level of object
hierarchy to be fetched?

Hibernate Interview Answer :
Yes, one can impose certain depth or level of object hierarchy to be fetched while using
outer join fetch strategy, by using the configuration setting as hibernate.max_fetch_depth
with some count number.

Hibernate Interview Question :
In case of highly concurrent database usage mode, can you set for all updates on table to
be executed based on primary key column of the table, for which column data to be updated?

Hibernate Interview Answer :
Yes, by using hibernate.order_updates as true or false for achieving/forcing this type of 
updates based on primary key column values.

Hibernate Interview Question :
Suppose you have encountered a situation whereby cluster aware second level cache is not 
performing properly or upto desired expectation level while working wiht Hibernate. 
Is there any setting that you can remember that can help by minimizing number of updates 
or object put calls, thus can help in increasing performance of read from cache in cluster
environment?

Hibernate Interview Answer :
hibernate.cache.use_minimal_puts setting in Hibernate configuration file, with a value
as true or false, would optimize Hibernate second-level cache by minimizing number of
additions/updations to objects those are being cached, thus minimizing overhead associated
with number of reads from database.

Hibernate Interview Question :
How can you log all seond-level cache related activities while using Hibernate Framework? 

Hibernate Interview Answer :
By using the Log category "org.hibernate.cache", one can obtain log related to Hibernate's
second-level cache activities.

Hibernate Interview Question :
What are the Transaction Strategies available with Hibernate Framework?

Hibernate Interview Answer :
Various transaction strategies available in Hibernate as such are for
JDBC, JTA and CMT with related TransactionFactories.

Hibernate Interview Question :
Does Hibernate as of latest version, provide support for use defined Transaction Factories?

Hibernate Interview Answer :
Yes, as of latest version of Hibernate Framework, custom/use defined/supplied Transaction
Factories can be used, by defining appropriate factory class file for the setting
"hibernate.transaction.factory_class."




Disclaim: These materials mentioned as above, are respective Author's own understanding of 
Hibernate Framework, for details and complete information, please refer to Hibernate web-site
http://www.hibernate.org/

If anything missed out , please let me know at techienjoy at yahoo . com
Hibernate Example on composite Primary key :
Example on using Hibernate Framework
to work with mapping using composite
Primary key.
Hibernate Interview Questions :
Interview Questions on Hibernate with answer.
Hibernate Join Example :
Using Table join explained with an example
while using Hibernate Framework.
Hibernate Component Property :
Hibernate Example on Component 
with source code explained.
Hibernate Insert Update control :
Hibernate Example on controlling
insert and update attributes
Hibernate Many to Many Mapping Example :
Many to many mapping example using Hibernate
Framework and a simple to follow steps.
Example on persisting Class Hierarchy :
Example on using Hibernate Framework
to persist Class Hierarchy into database.
Hibernate one to one mapping Example :
one to one mapping explained using an example
and Hibernate Framework.
Hibernate Bag Mapping Example :
class mapping using Bag Tag example using Hibernate
Framework and a simple to follow steps.
List of Examples on Hibernate :
List of example using Hibernate.
Hibernate one to many mapping Example :
one to many mapping explained using an example
and Hibernate Framework.
Class Hierarchy Mapping Example :
class hierarchy mapping example using Hibernate
Framework and a simple to follow steps.
Hibernate Example on Filter :
Example on using Filter using Hibernate Framework
to work with.
Hibernate Property Formula :
Hibernate Example on Property
Tag with ease to do code walk-through
Hibernate one to many mapping Example :
one to many mapping explained using an example
and Hibernate Framework.
Hibernate Named Query Example :
Named Query markup using an example
and Hibernate Framework.
Hibernate Example on Filter Criteria :
Example on using Filter Criteria
using Hibernate Framework to work with.
Hibernate class heirarchy mapping :
Hibernate Example on mapping
class hierarchy using various ways
of persisting into database
tables.
Hibernate Interceptor Example :
Example on using Interceptor using Hibernate Framework
with source code explained.
Hibernate Transaction on JBoss :
Explaining Transaction using Hibernate
on JBoss Application Server.


References :
Tags: Hibernate Class Hierarchy Persist example
Tags: Hibernate composite primary key
Tags: Hibernate criteria filter example
Tags: Hibernate Filter Example
Tags: Hibernate Interceptor example
Tags: Hibernate Interview Questions
Tags: Hibernate join example
Tags: Hibernate Many to Many Mapping Example
Tags: Hibernate mapping bag
Tags: Hibernate mapping class hierarchy table per subclass
Tags: Hibernate named query markup
Tags: Hibernate One to Many mapping example
Tags: Hibernate One To One Mapping Example
Tags: Hibernate scenario one to many
Tags: Hibernate transaction jboss
Tags: Hibernate



DISCLAIMER :
The content provided in this page is not warranted and/or guaranteed by techienjoy.com. 
techienjoy.com is not liable for any negative consequences that may result/arise from 
implementing directly/indirectly any information covered in these pages/articles/tutorials.

All contents of this site is/are written and provided on an "AS IS" basis,
without WARRANTIES or conditions of any kind, either express or implied, including, without
limitation, merchantability, or fitness for a particular purpose. You are solely responsible
for determining the appropriateness of using or refering this and assume any risks associated
with this.

In spite of all precautions taken to avoid any typo in these pages, there might be some 
issues like grammatical mistakes and typos being observed in these pages, techienjoy.com
extends sincerest apologies to all our visitors for the same.



Android Examples || Android Examples

© Copyright 2010-2012, TECHIENJOY, All Rights Reserved.      Privacy Policy     Disclaimer & Terms & Conditions