Hibernate Interview Questions and Answers
Hibernate Interview Questions and Answers
Hibernate Interview Questions and Answers
Hibernate is one of the most widely used ORM tool for Java applications. Its used a lot in enterprise
applications for database operations. So I decided to write a post about hibernate interview questions to
brush up your knowledge before the interview.
Whether you are fresher or experienced, having good knowledge or Hibernate ORM tool helps in cracking
interview. Here I am providing important hibernate interview questions with answers to help you brush up
your knowledge and impress your interviewer. Just like other interview questions posts, chances are that I
will be adding more questions to this list in future, so you might want to bookmark it for future reference.
Recently I have written a lot of posts on hibernate, most of them contains complete downloadable projects.
I will provide reference to them as and when needed and you can go through them to refresh your
knowledge.
http://www.journaldev.com/3633/hibernateinterviewquestionsandanswers 1/39
10/17/2016 HibernateInterviewQuestionsandAnswersJournalDev
Object-relational mapping or ORM is the programming technique to map application domain model
objects to the relational database tables. Hibernate is java based ORM tool that provides framework for
mapping application domain objects to the relational database tables and vice versa.
Hibernate provides reference implementation of Java Persistence API, that makes it a great choice as
ORM tool with bene ts of loose coupling. We can use Hibernate persistence API for CRUD operations.
Hibernate framework provide option to map plain old java objects to traditional database tables with
the use of JPA annotations as well as XML based con guration.
Similarly hibernate con gurations are exible and can be done from XML con guration le as well as
programmatically. For a quick overview of hibernate framework usage, you can go through Hibernate
Beginners Tutorial.
JPA speci cations is de ned with annotations in javax.persistence package. Using JPA annotation helps
us in writing implementation independent code.
1. Hibernate eliminates all the boiler-plate code that comes with JDBC and takes care of managing
resources, so we can focus on business logic.
2. Hibernate framework provides support for XML as well as JPA annotations, that makes our code
implementation independent.
3. Hibernate provides a powerful query language (HQL) that is similar to SQL. However, HQL is fully
object-oriented and understands concepts like inheritance, polymorphism and association.
http://www.journaldev.com/3633/hibernateinterviewquestionsandanswers 3/39
10/17/2016 HibernateInterviewQuestionsandAnswersJournalDev
4. Hibernate is an open source project from Red Hat Community and used worldwide. This makes it
a better choice than others because learning curve is small and there are tons of online
documentations and help is easily available in forums.
5. Hibernate is easy to integrate with other Java EE frameworks, its so popular that Spring
Framework provides built-in support for integrating hibernate with Spring applications.
6. Hibernate supports lazy initialization using proxy objects and perform actual database queries
only when its required.
7. Hibernate cache helps us in getting better performance.
8. For database vendor speci c feature, hibernate is suitable because we can also execute native
sql queries.
Overall hibernate is the best choice in current market for ORM tool, it contains all the features that you
will ever need in an ORM tool.
1. Hibernate removes a lot of boiler-plate code that comes with JDBC API, the code looks more
cleaner and readable.
2. Hibernate supports inheritance, associations and collections. These features are not present with
JDBC API.
3. Hibernate implicitly provides transaction management, in fact most of the queries cant be
executed outside transaction. In JDBC API, we need to write code for transaction management
using commit and rollback. Read more at JDBC Transaction Management.
4. JDBC API throws SQLException that is a checked exception, so we need to write a lot of try-catch
block code. Most of the times its redundant in every JDBC call and used for transaction
management. Hibernate wraps JDBC exceptions and throw JDBCException or
HibernateException un-checked exception, so we dont need to write code to handle it.
Hibernate built-in transaction management removes the usage of try-catch blocks.
5. Hibernate Query Language (HQL) is more object oriented and close to java programming
language. For JDBC, we need to write native sql queries.
6. Hibernate supports caching that is better for performance, JDBC queries are not cached hence
performance is low.
7. Hibernate provide option through which we can create database tables too, for JDBC tables must
exist in the database.
8. Hibernate con guration helps us in using JDBC like connection as well as JNDI DataSource for
connection pool. This is very important feature in enterprise application and completely missing
in JDBC API.
9. Hibernate supports JPA annotations, so code is independent of implementation and easily
replaceable with other ORM tools. JDBC code is very tightly coupled with the application.
http://www.journaldev.com/3633/hibernateinterviewquestionsandanswers 4/39
10/17/2016 HibernateInterviewQuestionsandAnswersJournalDev
1. javax.persistence.Entity: Used with model classes to specify that they are entity beans.
2. javax.persistence.Table: Used with entity beans to de ne the corresponding table name in
database.
3. javax.persistence.Access: Used to de ne the access type, either eld or property. Default value
is eld and if you want hibernate to use getter/setter methods then you need to set it to property.
4. javax.persistence.Id: Used to de ne the primary key in the entity bean.
5. javax.persistence.EmbeddedId: Used to de ne composite primary key in the entity bean.
6. javax.persistence.Column: Used to de ne the column name in database table.
7. javax.persistence.GeneratedValue: Used to de ne the strategy to be used for generation of
primary key. Used in conjunction with javax.persistence.GenerationType enum.
http://www.journaldev.com/3633/hibernateinterviewquestionsandanswers 5/39
10/17/2016 HibernateInterviewQuestionsandAnswersJournalDev
packagecom.journaldev.hibernate.model;
importjavax.persistence.Access;
importjavax.persistence.AccessType;
importjavax.persistence.Column;
importjavax.persistence.Entity;
importjavax.persistence.GeneratedValue;
importjavax.persistence.GenerationType;
importjavax.persistence.Id;
importjavax.persistence.OneToOne;
importjavax.persistence.Table;
importorg.hibernate.annotations.Cascade;
@Entity
@Table(name="EMPLOYEE")
@Access(value=AccessType.FIELD)
publicclassEmployee{
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(name="emp_id")
privatelongid;
@Column(name="emp_name")
privateStringname;
@OneToOne(mappedBy="employee")
@Cascade(value=org.hibernate.annotations.CascadeType.ALL)
privateAddressaddress;
//gettersettermethods
}
http://www.journaldev.com/3633/hibernateinterviewquestionsandanswers 6/39
10/17/2016 HibernateInterviewQuestionsandAnswersJournalDev
packagecom.journaldev.hibernate.model;
importjavax.persistence.Access;
importjavax.persistence.AccessType;
importjavax.persistence.Column;
importjavax.persistence.Entity;
importjavax.persistence.GeneratedValue;
importjavax.persistence.Id;
importjavax.persistence.OneToOne;
importjavax.persistence.PrimaryKeyJoinColumn;
importjavax.persistence.Table;
importorg.hibernate.annotations.GenericGenerator;
importorg.hibernate.annotations.Parameter;
@Entity
@Table(name="ADDRESS")
@Access(value=AccessType.FIELD)
publicclassAddress{
@Id
@Column(name="emp_id",unique=true,nullable=false)
@GeneratedValue(generator="gen")
@GenericGenerator(name="gen",strategy="foreign",parameters={
@Parameter(name="property",value="employee")})
privatelongid;
@Column(name="address_line1")
privateStringaddressLine1;
@OneToOne
@PrimaryKeyJoinColumn
privateEmployeeemployee;
//gettersettermethods
The internal state of a SessionFactory is immutable. Once it is created this internal state is set. This
internal state includes all of the metadata about Object/Relational Mapping.
http://www.journaldev.com/3633/hibernateinterviewquestionsandanswers 7/39
10/17/2016 HibernateInterviewQuestionsandAnswersJournalDev
SessionFactory also provide methods to get the Class metadata and Statistics instance to get the stats
of query executions, second level cache details etc.
Session provide methods to perform create, read, update and delete operations for a persistent object.
We can execute HQL queries, SQL native queries and create criteria using Session object.
<propertyname="hibernate.current_session_context_class">thread</property>
Hibernate SessionFactory openSession() method always opens a new session. We should close this
session object once we are done with all the database operations. We should open a new session for
each request in multi-threaded environment.
There is another method openStatelessSession() that returns stateless session, for more details with
examples please read Hibernate openSession vs getCurrentSession.
http://www.journaldev.com/3633/hibernateinterviewquestionsandanswers 8/39
10/17/2016 HibernateInterviewQuestionsandAnswersJournalDev
1. get() loads the data as soon as its called whereas load() returns a proxy object and loads data
only when its actually required, so load() is better because it support lazy loading.
2. Since load() throws exception when data is not found, we should use it only when we know data
exists.
3. We should use get() when we want to make sure data exists in the database.
For clari cation regarding the di erences, please read Hibernate get vs load.
Hibernate rst level cache is associated with the Session object. Hibernate rst level cache is enabled
by default and there is no way to disable it. However hibernate provides methods through which we
can delete selected objects from the cache or clear the cache completely.
Any object cached in a session will not be visible to other sessions and when the session is closed, all
the cached objects will also be lost.
16. How to con gure Hibernate Second Level Cache using EHCache?
EHCache is the best choice for utilizing hibernate second level cache. Following steps are required to
enable EHCache in hibernate application.
Add hibernate-ehcache dependency in your maven project, if its not maven then add
corresponding jars.
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernateehcache</artifactId>
<version>4.3.5.Final</version>
</dependency>
http://www.journaldev.com/3633/hibernateinterviewquestionsandanswers 9/39
10/17/2016 HibernateInterviewQuestionsandAnswersJournalDev
<property
name="hibernate.cache.region.factory_class">org.hibernate.cache.ehcache.EhCacheRegionFacto
<!Forsingletonfactory>
<!<property
name="hibernate.cache.region.factory_class">org.hibernate.cache.ehcache.SingletonEhCacheRe
>
<!enablesecondlevelcacheandquerycache>
<propertyname="hibernate.cache.use_second_level_cache">true</property>
<propertyname="hibernate.cache.use_query_cache">true</property>
<property
name="net.sf.ehcache.configurationResourceName">/myehcache.xml</property>
Create EHCache con guration le, a sample le myehcache.xml would look like below.
http://www.journaldev.com/3633/hibernateinterviewquestionsandanswers 10/39
10/17/2016 HibernateInterviewQuestionsandAnswersJournalDev
<?xmlversion="1.0"encoding="UTF8"?>
<ehcachexmlns:xsi="http://www.w3.org/2001/XMLSchemainstance"
xsi:noNamespaceSchemaLocation="ehcache.xsd"updateCheck="true"
monitoring="autodetect"dynamicConfig="true">
<diskStorepath="java.io.tmpdir/ehcache"/>
<defaultCachemaxEntriesLocalHeap="10000"eternal="false"
timeToIdleSeconds="120"timeToLiveSeconds="120"
diskSpoolBufferSizeMB="30"
maxEntriesLocalDisk="10000000"diskExpiryThreadIntervalSeconds="120"
memoryStoreEvictionPolicy="LRU"statistics="true">
<persistencestrategy="localTempSwap"/>
</defaultCache>
<cachename="employee"maxEntriesLocalHeap="10000"eternal="false"
timeToIdleSeconds="5"timeToLiveSeconds="10">
<persistencestrategy="localTempSwap"/>
</cache>
<cachename="org.hibernate.cache.internal.StandardQueryCache"
maxEntriesLocalHeap="5"eternal="false"timeToLiveSeconds="120">
<persistencestrategy="localTempSwap"/>
</cache>
<cachename="org.hibernate.cache.spi.UpdateTimestampsCache"
maxEntriesLocalHeap="5000"eternal="true">
<persistencestrategy="localTempSwap"/>
</cache>
</ehcache>
Annotate entity beans with @Cache annotation and caching strategy to use. For example,
importorg.hibernate.annotations.Cache;
importorg.hibernate.annotations.CacheConcurrencyStrategy;
@Entity
@Table(name="ADDRESS")
@Cache(usage=CacheConcurrencyStrategy.READ_ONLY,region="employee")
publicclassAddress{
http://www.journaldev.com/3633/hibernateinterviewquestionsandanswers 11/39
10/17/2016 HibernateInterviewQuestionsandAnswersJournalDev
Thats it, we are done. Hibernate will use the EHCache for second level caching, read Hibernate
EHCache Example for a complete example with explanation.
SEND ME NOW!
1. Transient: When an object is never persisted or associated with any session, its in transient state.
Transient instances may be made persistent by calling save(), persist() or saveOrUpdate().
Persistent instances may be made transient by calling delete().
2. Persistent: When an object is associated with a unique session, its in persistent state. Any
instance returned by a get() or load() method is persistent.
3. Detached: When an object is previously persistent but not associated with any session, its in
detached state. Detached instances may be made persistent by calling update(), saveOrUpdate(),
lock() or replicate(). The state of a transient or detached instance may also be made persistent as
a new persistent instance by calling merge().
Hibernate persist is similar to save with transaction. I feel its better than save because we cant use it
outside the boundary of transaction, so all the object mappings are preserved. Also persist doesnt
return the generated id immediately, so data persistence happens when needed.
http://www.journaldev.com/3633/hibernateinterviewquestionsandanswers 12/39
10/17/2016 HibernateInterviewQuestionsandAnswersJournalDev
Hibernate saveOrUpdate results into insert or update queries based on the provided data. If the data is
present in the database, update query is executed. We can use saveOrUpdate() without transaction
also, but again you will face the issues with mapped objects not getting saved if session is not ushed.
Free eBook: Java Design Patterns (130 Pages) Download Now!
For example usage of these methods, read Hibernate save vs persist.
Your email address please..
If we are using Hibernate framework to load collection data from database, we can use its Criteria API
to use order by clause to get ordered list. Below code snippet shows you how to get it.
List<Employee>empList=session.createCriteria(Employee.class)
.addOrder(Order.desc("id")).list();
Ordered list is better than sorted list because the actual sorting is done at database level, that is fast
and doesnt cause memory issues.
1. Bag
2. Set
3. List
4. Array
5. Map
http://www.journaldev.com/3633/hibernateinterviewquestionsandanswers 13/39
10/17/2016 HibernateInterviewQuestionsandAnswersJournalDev
Hibernate query language is case-insensitive except for java class and variable names. So SeLeCT is
the same as sELEct is the same as SELECT, but com.journaldev.model.Employee is not same as
com.journaldev.model.EMPLOYEE.
The HQL queries are cached but we should avoid it as much as possible, otherwise we will have to
take care of associations. However its a better choice than native sql query because of Object-
Oriented approach. Read more at HQL Example.
This is an optional feature and requires additional steps in code. This is only useful for queries that are
run frequently with the same parameters. First of all we need to con gure below property in hibernate
con guration le.
<propertyname="hibernate.cache.use_query_cache">true</property>
And in code, we need to use setCacheable(true) method of Query, quick example looks like below.
Queryquery=session.createQuery("fromEmployee");
query.setCacheable(true);
query.setCacheRegion("ALL_EMP");
http://www.journaldev.com/3633/hibernateinterviewquestionsandanswers 14/39
10/17/2016 HibernateInterviewQuestionsandAnswersJournalDev
Hibernate provide option to execute native SQL queries through the use of SQLQuery object.
For normal scenarios, it is however not the recommended approach because we loose bene ts related
Free eBook: Java Design Patterns (130 Pages) Download Now!
to hibernate association and hibernate rst level caching. Read more at Hibernate Native SQL Query
Your email address please..
Example.
SEND ME NOW!
28. What is the bene t of native sql query support in hibernate?
Native SQL Query comes handy when we want to execute database speci c queries that are not
supported by Hibernate API such as query hints or the CONNECT keyword in Oracle Database.
Hibernate Named Queries can be de ned in Hibernate mapping les or through the use of JPA
annotations @NamedQuery and @NamedNativeQuery.
However one of the major disadvantage of Named query is that its hard to debug, because we need
to nd out the location where its de ned.
Criteria API provides Projection that we can use for aggregate functions such as sum(), min(),
max() etc.
Criteria API can be used with ProjectionList to fetch selected columns only.
Criteria API can be used for join queries by joining multiple tables, useful methods are
createAlias(), setFetchMode() and setProjection()
Criteria API can be used for fetching results with conditions, useful methods are add() where we
can add Restrictions.
http://www.journaldev.com/3633/hibernateinterviewquestionsandanswers 15/39
10/17/2016 HibernateInterviewQuestionsandAnswersJournalDev
Criteria API provides addOrder() method that we can use for ordering the results.
<propertyname="hibernate.show_sql">true</property>
However we should use it only in Development or Testing environment and turn it o in production
environment.
Overall hibernate transaction management is better than JDBC transaction management because we
dont need to rely on exceptions for rollback. Any exception thrown by session methods automatically
rollback the transaction.
http://www.journaldev.com/3633/hibernateinterviewquestionsandanswers 16/39
10/17/2016 HibernateInterviewQuestionsandAnswersJournalDev
When we have relationship between entities, then we need to de ne how the di erent operations will
a ect the other entity. This is done by cascading and there are di erent types of it.
SEND ME NOW!
importorg.hibernate.annotations.Cascade;
@Entity
@Table(name="EMPLOYEE")
publicclassEmployee{
@OneToOne(mappedBy="employee")
@Cascade(value=org.hibernate.annotations.CascadeType.ALL)
privateAddressaddress;
Note that Hibernate CascadeType enum constants are little bit di erent from JPA
javax.persistence.CascadeType, so we need to use the Hibernate CascadeType and Cascade
annotations for mappings, as shown in above example.
Commonly used cascading types as de ned in CascadeType enum are:
1. None: No Cascading, its not a type but when we dont de ne any cascading then no operations
in parent a ects the child.
2. ALL: Cascades save, delete, update, evict, lock, replicate, merge, persist. Basically everything
3. SAVE_UPDATE: Cascades save and update, available only in hibernate.
4. DELETE: Corresponds to the Hibernate native DELETE action, only in hibernate.
5. DETATCH, MERGE, PERSIST, REFRESH and REMOVE for similar operations
6. LOCK: Corresponds to the Hibernate native LOCK action.
7. REPLICATE: Corresponds to the Hibernate native REPLICATE action.
Add log4j dependencies for maven project, if not maven then add corresponding jar les.
Create log4j.xml con guration le or log4j.properties le and keep it in the classpath. You can
keep le name whatever you want because we will load it in next step.
For standalone projects, use static block to con gure log4j using DOMConfigurator or
PropertyConfigurator. For web applications, you can use ServletContextListener to con gure it.
Thats it, our setup is ready. Create org.apache.log4j.Logger instance in the java classes and start
logging. For complete example code, you should go through Hibernate log4j example and Servlet
http://www.journaldev.com/3633/hibernateinterviewquestionsandanswers 17/39
10/17/2016 HibernateInterviewQuestionsandAnswersJournalDev
log4j example.
<property
name="hibernate.connection.datasource">java:comp/env/jdbc/MyLocalDB</property>
http://www.journaldev.com/3633/hibernateinterviewquestionsandanswers 18/39
10/17/2016 HibernateInterviewQuestionsandAnswersJournalDev
One other bene t of HibernateTemplate was exception translation but that can be achieved easily by
using @Repository annotation with service classes, shown in above spring mvc example. This is a trick
question to judge your knowledge and whether you are aware of recent developments or not.
Free eBook: Java Design Patterns (130 Pages) Download Now!
Your email How to integrate Hibernate with Servlet or Struts2 web
41. address please..
applications? SEND ME NOW!
Domain Model Pattern An object model of the domain that incorporates both behavior and
data.
Data Mapper A layer of Mappers that moves data between objects and a database while
keeping them independent of each other and the mapper itself.
Proxy Pattern for lazy loading
Factory pattern in SessionFactory
Always check the primary key eld access, if its generated at the database layer then you should
not have a setter for this.
By default hibernate set the eld values directly, without using setters. So if you want hibernate to
use setters, then make sure proper access is de ned as @Access(value=AccessType.PROPERTY).
If access type is property, make sure annotations are used with getter methods and not setter
methods. Avoid mixing of using annotations on both led and getter methods.
Use native sql query only when it cant be done using HQL, such as using database speci c
feature.
If you have to sort the collection, use ordered list rather than sorting it using Collection API.
Use named queries wisely, keep it at a single place for easy debugging. Use them for commonly
used queries only. For entity speci c query, you can keep them in the entity bean itself.
For web applications, always try to use JNDI DataSource rather than con guring to create
connection in hibernate.
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 dont get bene t of lazy
loading.
Do not treat exceptions as recoverable, roll back the Transaction and close the Session. If you do
not do this, Hibernate cannot guarantee that in-memory state accurately represents the
persistent state.
http://www.journaldev.com/3633/hibernateinterviewquestionsandanswers 19/39
10/17/2016 HibernateInterviewQuestionsandAnswersJournalDev
Prefer DAO pattern for exposing the di erent methods that can be used with entity bean
Prefer lazy fetching for associations
Validation is a cross cutting task, so we should try to keep it apart from our business logic. Thats why
JSR303 and JSR349 provides speci cation for validating a bean by using annotations. Hibernate
Validator provides the reference implementation of both these bean validation specs. Read more at
Hibernate Validation Example.
Thats all for Hibernate Interview Questions and Answers, I hope it will help you for interview as a fresher or
experienced person. Please let me know if I have missed any important question here, I will add that to the
list.
About Pankaj
If you have come this far, it means that you liked what you are reading. Why not reach little more and connect
with me directly on Google Plus, Facebook or Twitter. I would love to hear your thoughts and opinions on my
articles directly.
Recently I started creating video tutorials too, so do check out my videos on Youtube.
Comments
http://www.journaldev.com/3633/hibernateinterviewquestionsandanswers 20/39
10/17/2016 HibernateInterviewQuestionsandAnswersJournalDev
HibernateDaoSupport and HibernateTemplate should not be used for any further development.
This is what to be instructed in the latest document from Spring o cials.
Reply
Priyesh says
FEBRUARY 12, 2016 AT 1:37 AM
Hi Pankaj,
Good article for interview preparation, but in question# 10 have little doubt if SessionFactory is thread
safe than how Multiple threads can access it simultaneously to get Session instances.
Reply
Reply
Naveen says
SEPTEMBER 26, 2016 AT 4:28 AM
ThreadSafe meansthe SessionFactory objects state cannot be changed by any thread accessing it.
It does not mean threads cant access simultaneously.
Multiple threads can safely access a ThreadSafe object-SessionFactory.
Reply
prakash says
http://www.journaldev.com/3633/hibernateinterviewquestionsandanswers 21/39
10/17/2016 HibernateInterviewQuestionsandAnswersJournalDev
OCTOBER 30, 2015 AT 2:53 AM
once the data is present(come to) in proxy object..then after the data was changedthen the same
request is comehereFree eBook:isJava
question the Design
data is Patterns (130proxy
taken from Pages)
orDownload
database..Now!
Your email address please..
Reply
SEND ME NOW!
Vivek says
DECEMBER 13, 2015 AT 10:17 PM
Proxy Object.
Reply
Question I was asked in an interview : can you drop a table using hibernate/hql? and how ?
Thanks.
Reply
Reply
DBA says
OCTOBER 4, 2015 AT 11:48 AM
Hi Pankaj,
http://www.journaldev.com/3633/hibernateinterviewquestionsandanswers 22/39
10/17/2016 HibernateInterviewQuestionsandAnswersJournalDev
Reply
Free eBook: Java Design Patterns (130 Pages) Download Now!
Your email address please..
SEND ME NOW!
Himansu Nayak says
AUGUST 21, 2016 AT 10:57 AM
Reply
Reply
Can we maintain two DB con gurations in single hibernate con guration le? If yes, How?
If No, Why?
Reply
Sevak says
JANUARY 19, 2016 AT 6:36 AM
Reply
http://www.journaldev.com/3633/hibernateinterviewquestionsandanswers 23/39
10/17/2016 HibernateInterviewQuestionsandAnswersJournalDev
AUGUST 21, 2016 AT 10:40 AM
If you go with Hibernate-JPA then you can con gure multiple persistence-unit in persistence.xml.
Each persistenceFree
uniteBook:
de nesJava Design Patterns (130 Pages) Download Now!
a datasource.
Your email address please..
Reply
SEND ME NOW!
Yegor says
AUGUST 26, 2015 AT 2:17 AM
Reply
Swetha says
JULY 6, 2015 AT 1:10 AM
Reply
vishwas says
JUNE 22, 2015 AT 4:06 PM
Reply
hi sir
its very useful for the interview
http://www.journaldev.com/3633/hibernateinterviewquestionsandanswers 24/39
10/17/2016 HibernateInterviewQuestionsandAnswersJournalDev
Reply
Excellent but
Avoid Many-to-Many relationships, it can be easily implemented using bidirectional One-to-Many and
Many-to-One relationships.
Reply
Thanks a lot Pankaj,amazing collections of Questions.It covered all the questions.However,i wanted to
know regarding one question:-
Q) How to alter Database speci c con guration information in Hibernate without restarting the entire
application?
Regards,
Kapil Bhardwaj
Reply
Gagan says
MAY 22, 2015 AT 2:19 AM
Nice article
Reply
Bhaskar says
http://www.journaldev.com/3633/hibernateinterviewquestionsandanswers 25/39
10/17/2016 HibernateInterviewQuestionsandAnswersJournalDev
MAY 3, 2015 AT 8:01 AM
hai sir
Free eBook:
This every help-full interview sir Java Design Patterns (130 Pages) Download Now!
Your email address please..
Thanks sir
SEND ME NOW!
Bhaskar
Reply
This was really very concise but very well explained article. I have bookmarked this page and I can
always refer this page with con dence if I want to prepare for Hibernate interview.
Keep it up !!
Reply
nahar says
APRIL 3, 2015 AT 11:43 AM
really good sir it helping me but sir acctullly you havent give any example please sir next time provide
some example ,
Reply
Swadhi says
APRIL 2, 2015 AT 8:15 PM
really appreciate the e ort! could you add questions on Hibernate inheritance with examples? thanks!
Reply
http://www.journaldev.com/3633/hibernateinterviewquestionsandanswers 26/39
10/17/2016 HibernateInterviewQuestionsandAnswersJournalDev
Reply
Reply
Abhishek says
MARCH 4, 2015 AT 1:09 AM
Reply
Reply
Thanks Pankaj, this blog really helps to freshers as well as for experience person.
http://www.journaldev.com/3633/hibernateinterviewquestionsandanswers 27/39
10/17/2016 HibernateInterviewQuestionsandAnswersJournalDev
Keep it up.
Reply
Free eBook: Java Design Patterns (130 Pages) Download Now!
Your email address please..
SEND ME NOW!
kolem says
FEBRUARY 14, 2015 AT 11:44 AM
Perfect article! I used it for interview preparation, it helped me to recover knowledges and I also learnt
several new things. Many thanks!
Reply
Rohan P. says
FEBRUARY 14, 2015 AT 10:55 AM
I have been following all of your Interview related question and answer, its lot much helpful..
In an recent interview, I have been asked a question How to create two same tables in di erent
Schemas by using single con guration le? And How to use single con guration le to work with two
di erent databases?, I was clueless for these question & these questions put me out of the interview.
Can you please answer them.
Thank you.
Reply
Pankaj says
FEBRUARY 14, 2015 AT 8:36 PM
You can create multiple session-factory con gurations with di erent name and then get them in the
Code by name, such as
Reply
http://www.journaldev.com/3633/hibernateinterviewquestionsandanswers 28/39
10/17/2016 HibernateInterviewQuestionsandAnswersJournalDev
Raghav says
AUGUST 3, 2015 AT 5:31 PM
Free eBook: Java Design Patterns (130 Pages) Download Now!
Your email address
Hiplease..
Pankaj,
Could you please give one example for How to create two same tables in di erent Schemas
SEND ME NOW!
by using single con guration le? And How to use single con guration le to work with two
di erent databases? Thanks.
Reply
Reply
nithu says
JANUARY 8, 2015 AT 5:43 AM
Reply
nirbi says
JANUARY 2, 2015 AT 9:35 AM
Sorry for interrupting you ,but i have a doubt related to named queries that u have mentioned above.
we are centralizing the query in one location for multi uses in our application instead of scattering it but
what are the the cons or draw backs of it???
Reply
http://www.journaldev.com/3633/hibernateinterviewquestionsandanswers 29/39
10/17/2016 HibernateInterviewQuestionsandAnswersJournalDev
I think answer to Question 14 is incorrect. Both get and load supports lazy loading. The only di erence is
Free eBook: Java Design Patterns (130 Pages) Download Now!
that
Your email get returns
address null if the row is not available corresponding to passed id while load throws an
please..
exception.
SEND ME NOW!
Reply
I was wrong. I misunderstood the proxy object thing but now its clear. The answer is perfectly
correct. Thanks
Reply
Reply
ali says
DECEMBER 1, 2014 AT 9:58 PM
Hi Pankaj
As this link shows Eclipselink is the RI of the Java Persistance API not the Hibernate.
regards
Reply
http://www.journaldev.com/3633/hibernateinterviewquestionsandanswers 30/39
10/17/2016 HibernateInterviewQuestionsandAnswersJournalDev
Sorry, but we also can create tables through JDBC native SQL queries. For example:
StringcreateQueryText="CREATETABLE`Employee`(\n"+
"IDintnotnullprimarykey\n"+
");";
Statementstatement=connection.createStatement();
statement.execute(createQueryText);
Reply
Pankaj says
NOVEMBER 28, 2014 AT 8:49 PM
Yes we can, but in Hibernate we can con gure it to create if table doesnt exist. If you are using JDBC
in any application, the code will try to create table every time its executed and throw exception, yes
we can argue that we can catch and ignore it. But its very clumsy and not a preferred approach.
Reply
Gopal says
NOVEMBER 16, 2014 AT 11:18 PM
Could you please add N+1 update problem question and explanation.
Thanks
Reply
http://www.journaldev.com/3633/hibernateinterviewquestionsandanswers 31/39
10/17/2016 HibernateInterviewQuestionsandAnswersJournalDev
SEND ME NOW!
Reply
Bhavani says
OCTOBER 20, 2014 AT 2:26 AM
Reply
Hi,
can you please give a detailed explanation , how to call a procedure in Hibernate 3.0 and struts 2.0 , I am
unable to do it ?
Reply
varalakshmi says
FEBRUARY 17, 2015 AT 11:05 PM
session.createSQLQuery(CALL procedure_name(:param)).setParameter(param,
param).executeUpdate()
Reply
http://www.journaldev.com/3633/hibernateinterviewquestionsandanswers 32/39
10/17/2016 HibernateInterviewQuestionsandAnswersJournalDev
OCTOBER 17, 2014 AT 2:40 AM
Great Work
FreeQuestion
Mostly all the Hibernate eBook: Java DesigninPatterns
covered (130 Pages) Download Now!
this post..
Your email address please..
#Hibernate
SEND ME NOW!
Reply
Deepak says
OCTOBER 16, 2014 AT 11:45 PM
Reply
Ram says
SEPTEMBER 30, 2014 AT 5:59 AM
Reply
arun says
SEPTEMBER 20, 2014 AT 12:56 AM
Reply
rohit says
SEPTEMBER 3, 2014 AT 9:11 PM
Reply
http://www.journaldev.com/3633/hibernateinterviewquestionsandanswers 33/39
10/17/2016 HibernateInterviewQuestionsandAnswersJournalDev
Reply
Reply
Leave a Reply
Your email address will not be published. Required elds are marked *
Comment
Name *
Email *
Website
http://www.journaldev.com/3633/hibernateinterviewquestionsandanswers 34/39
10/17/2016 HibernateInterviewQuestionsandAnswersJournalDev
POST COMMENT
SEND ME NOW!
CONNECT WITH US
Hibernate Tutorial
Hibernate Example
Hibernate SessionFactory
HQL Example
Hibernate Criteria
Hibernate SQL
http://www.journaldev.com/3633/hibernateinterviewquestionsandanswers 35/39
10/17/2016 HibernateInterviewQuestionsandAnswersJournalDev
Hibernate Log4J
Hibernate Validator
Free eBook: Java Design Patterns (130 Pages) Download Now!
Your email address please.. Hibernate Tomcat DataSource
SEND ME NOW!
Hibernate Mapping
Hibernate One to One Mapping
Hibernate Caching
Hibernate Cache
Hibernate EHCache
Hibernate Integrations
Hibernate Spring
Hibernate Struts 2
Hibernate Primefaces
Miscellaneous
Hibernate Tools Eclipse Plugin
Access to DialectResolutionInfo
http://www.journaldev.com/3633/hibernateinterviewquestionsandanswers 36/39
10/17/2016 HibernateInterviewQuestionsandAnswersJournalDev
http://www.journaldev.com/3633/hibernateinterviewquestionsandanswers 37/39
10/17/2016 HibernateInterviewQuestionsandAnswersJournalDev
SEND ME NOW!
Common Job Interview Questions
Miscellaneous
Java ClassLoader
STAY UPDATED!
Name
E-Mail Address
I AM IN!
RECOMMENDED TUTORIALS
http://www.journaldev.com/3633/hibernateinterviewquestionsandanswers 38/39
10/17/2016 HibernateInterviewQuestionsandAnswersJournalDev
2016 Privacy Policy Don't copy, it's Bad Karma Powered by WordPress
http://www.journaldev.com/3633/hibernateinterviewquestionsandanswers 39/39