Sunteți pe pagina 1din 4

7/15/13

10 Hibernate InterviewQuestions and Answers for Java J2EE Programmers

Javarevisited Blog about Java Program Tutorial Example How to, Unix Linux commands, Interview Questions, FIX
Javarevisited
Blog about Java Program Tutorial Example How to, Unix Linux commands, Interview Questions, FIX Protocol, Tibco RV tutorials, Equities trading system, MySQL
► PDF Java
► Interview
► Java Hibernate
► Hibernate JBoss
T UES DA Y , MA Y
7, 2013
Search
10 Hibernate Interview Questions and Answers for Java J2EE Programmers
Recent Posts
Hibernate Interview Questions are asked on Java J2EE Interviews,
mostly for web based enterprise application development role.
Success and acceptability of Hibernate framework on Java world
has made it one of the most popular Object Relational Mapping
(ORM) solution in Java technology stack. Hibernate frees you from
database specific coding and allows you to focus more on utilizing
powerful object oriented design principle to implement core
business logic. By using Hibernate you can switch between
database rather easily and also take advantage of out of box
caching facilities provided by Hibernate, in terms of second level
cache and query cache. As you know most of Java interview not
only contains questions from core Java, but also from other Java
framework e.g. questions from Spring Framework or Struts
interview questions, based upon projects requirements. Its good to
prepare both Spring and Hibernate questions quite well, if you are going to work on a project which uses Hibernate as
ORM. Check JD or Job description ,and if you see word Hibernate anywhere, get ready to face some Hibernate questions.
When to make a method static in Java
developers Top 5 JQuery - Best books of lot for Beginners and Web
web How applications SSL, HTTPS and Certificates works in Java
for and Role Authorization MVC, based Mapping Access LDAP control Groups using Spring to Authorities Security
10 Answers Java Exception and Error Interview Questions
5 Java must Design read books patterns to learn - Best Object of lot Oriented and
Interview Question on Hibernate Framework
Here is my list of Hibernate interview question, which I have collected from friends and colleagues.
Hibernate is a popular Object Relational Mapping framework and good knowledge of advantages
offered by Hibernate along with Hibernate Session API is key to do well in any Hibernate Interview.
Difference between get and load in Hibernate?
get vs load is one of the most frequently asked Hibernate Interview question, since correct
understanding of both get()and load()is require to effectively using Hibernate. Main difference between get and load
is that, get will hit the database if object is not found in the cache and returned completely initialized object, which may
involve several database call while load()method can return proxy, if object is not found in cache and only hit database if
any method other than getId()is called. This can save lot of performance in some cases. You can also see difference
between get and load in Hibernate for more differences and detailed discussion on this question.
Difference between save, persist and saveOrUpdate methods in Hibernate?
After get vs load, this is another Hibernate Interview question which appears quite often. All three methods i.e. save(),
saveOrUpdate()and persist()is used to save objects into database, but has subtle differences e.g. save()can only
INSERT records but saveOrUpdate()can either INSERT or UPDATE records. Also, return type of save()is a
Serializableobject, while return type of persist() method is void. You can also check save vs persist vs
saveOrUpdate for complete differences between them in hibernate.
What is named SQL query in Hibernate?
This Hibernate Interview question is related to query functionality provided by Hibernate. Named queries are SQL queries
which are defined in mapping document using <sql-query>tag and called using Session.getNamedQuery()method.
Named query allows you to refer a particular query by the name you provided, by the way you can define named query in
hibernate either by using annotations or xml mapping file, as I said above. @NameQueryis used to define single named
query and @NameQueriesis used to define multiple named query in hibernate.
Follow Us
What is SessionFactory in Hibernate? is SessionFactory thread-safe?
Another common Interview questions related to Hibernate framework. SessionFactoryas name suggest is a factory to
create hibernate Sessionobjects. SessionFactory is often built during start-up and used by application code to get
session object. It acts as single data store and its also thread-safe so that multiple thread can use same
SessionFactory. Usually a Java JEE application has just one SessionFactory, and individual threads, which are
servicing client’s request obtain hibernate Session instances from this factory, that’s why any implementation of
SessionFactoryinterface must be thread-safe. Also internal state of SessionFactory, which contains all meta data
about Object/Relational mapping is Immutable and can not be changed once created.
FFoollllooww @@jjaavviinnppaauull
3,239 follow ers
Javarevisited on
Follow
+3,257
What is Session in Hibernate? Can we share single Session among multiple threads in Hibernate?
This is usually asked as follow-up question of previous Hibernate Interview question. After SessionFactoryits time for
Session. Sessionrepresent a small unit of work in Hibernate, they maintain connection with database and they are not
thread-safe, it means you can not share Hibernate Session between multiple threads. Though Sessionobtains
database connection lazily it's good to close session as soon as you are done with it.

7/15/13

10 Hibernate InterviewQuestions and Answers for Java J2EE Programmers

What is difference between sorted and ordered collection in hibernate? This is one of the easy Hibernate interview question you ever face. A sorted collection is sorted in memory by using Java Comparator, while a ordered collection uses database's orderbyclause for ordering. For large data set it's better to use ordered collection to avoid any OutOfMemoryError in Java, by trying to sort them in memory.

What is difference between transient, persistent and detached object in Hibernate? In Hibernate, Object can remain in three state transient, persistentor detached. An object which is associated with Hibernate session is called persistentobject. Any change in this object will reflect in database based upon your flush strategy i.e. automatic flush whenever any property of object change or explicit flushing by calling Session.flush() method. On the other hand if an object which is earlier associated with Session,but currently not associated with it are called detachedobject. You can reattach detached object to any other session by calling either update()or saveOrUpdate()method on that session. Transient objects are newly created instance of persistence class, which is never associated with any Hibernate Session. Similarly you can call persist()or save()methods to make transient object persistent. Just remember, here transient doesn’t represent transient keyword in Java, which is altogether different thing.

What does Session lock() method do in Hibernate? This one is one of the tricky Hibernate Interview question, because Session's lock()method reattach object without synchronizing or updating with database. So you need to be very careful while using lock()method. By the way you can always use Session's update()method to sync with database during reattachment. Some time this Hibernate question is also asked as what is difference between Session's lock() and update() method. You can use this key point to answer that question as well.

What is Second level Cache in Hibernate? This is one of the first interview question related to caching in Hibernate, you can expect few more. Second level Cache is maintained at SessionFactorylevel and can improve performance by saving few database round trip. Another worth noting point is that second level cache is available to whole application rather than any particular session.

What is query cache in Hibernate ? This question, Some times asked as a follow-up of last Hibernate Interview question, QueryCacheactually stores result of sql query for future calls. Query cache can be used along with second level cache for improved performance. Hibernate support various open source caching solution to implement Query cache e.g. EhCache.

Why it's important to provide no argument constructor in Hibernate Entities? Every Hibernate Entity class must contain a no argument constructor, because Hibernate framework creates instance of them using Reflection API, by calling Class.newInstance()method. This method will throw InstantiationException if it doesn't found no argument constructor inside Entity class.

Can we make an Hibernate Entity Class final? Yes, you can make an Hibernate Entity class final, but that's not a good practice. Since Hibernate uses proxy pattern for performance improvement in case of lazy association, by making an entity final, Hibernate will no longer be able to use proxy, because Java doesn't allow extension of final class, thus limiting your performance improvement options. Though, you can avoid this penalty, if your persistent class is an implementation of interface, which declares all public methods defined in Entity class.

That's all on this list of Hibernate Interview questions and answer for Java developers. No one can doubt popularity of Hibernate as ORM solution and if you are going for a Java J2EE position, you can expect questions from Hibernate. Especially Spring and Hibernate are two most popular Java framework in JEE space. Don't forget to share any other Hibernate Interview Question, which you have been asked and good enough to share with Java community.

been asked and good enough to share with Java community. Related Interview questions articles from Javarevisited

Related Interview questions articles from Javarevisited Blog

15

Java Multithreading Questions for Senior developers

10

advanced Java String Interview Questions with answers

Top 10 Garbage Collection Interview Questions for Java Programmers Java Collections Questions with Answers for 2 to 4 years experienced programmers Java Generics Interview Questions with Answers for practice

20 Object oriented design Interview Questions for Java developers

Please share with your friends if like this article

Please share with your friends if like this article Hibernate interview questions Business Ideas Most popular

Hibernate interview questions

Business Ideas

Most popular sites

Please share with your friends if like this article Hibernate interview questions Business Ideas Most popular
interview questions Business Ideas Most popular sites You might like: Difference between get and load in

You might like:

Difference between get and load in HibernateYou might like: 10 Object Oriented Design Principles Java Programmer should know Why Enum Singleton are

10 Object Oriented Design Principles Java Programmer should knowYou might like: Difference between get and load in Hibernate Why Enum Singleton are better in

Why Enum Singleton are better in Java10 Object Oriented Design Principles Java Programmer should know Top 10 Collection Interview Questions Answers in

Top 10 Collection Interview Questions Answers in Javaload in Hibernate 10 Object Oriented Design Principles Java Programmer should know Why Enum Singleton are

Top 10 Collection Interview Questions Answers in Java Javarevisited Like 4,660 people like Javarevisited .
Javarevisited Like 4,660 people like Javarevisited . Facebook social plugin Recommendd Books Java Programming and
Javarevisited Like 4,660 people like Javarevisited . Facebook social plugin Recommendd Books Java Programming and

Javarevisited

Like
Like

4,660 people like Javarevisited.

Javarevisited Like 4,660 people like Javarevisited . Facebook social plugin Recommendd Books Java Programming and Design

Facebook social pluginJavarevisited Like 4,660 people like Javarevisited . Recommendd Books Java Programming and Design Pattern Spring

Recommendd Books

Java Programming and Design Pattern Spring Framework

Subscribe by email:

Subscribe
Subscribe

By Javin Paul Subscribe To This Blog Free

Posts
Posts
Comments
Comments

Followers

Join this site
Join this site

w ith Google Friend Connect More »

Members (1250)

site w ith Google Friend Connect More » Members (1250) Already a member? Sign in  

Already a member? Sign in

Blog Archive

2013 (97)

July (4)

June (9)

May (14) How Two to in Check if Integer Number is Power of

Java NullPointerE Tips and Best practices to avoid

Which were Programming g Book would you buy if you

10 Quest XSLT or XML, XSL transformation Interview

7/15/13

10 Hibernate InterviewQuestions and Answers for Java J2EE Programmers

SED Command Examples in UNIX and Linux, Recommended by Find and R Posted by Javin
SED Command Examples in UNIX and Linux,
Recommended by
Find and R
Posted by Javin Paul at 5:43 AM
+24
Recommend this on Google
How to Generate Random Numbers in Java
between Ran
Labels: core java, hibernate, interview questions, J2EE
Difference between Abstract class vs Interface
in
Working at home
Work in home
Home Work
Work Home
At home work
How to Find if Linked List contains Loops or
Cycle
Performance Review
Databases
Hibernate interview questions
Business Ideas
Difference between LEFT and RIGHT OUTER
Joins in S
12 comments:
Migrating SQL Query from Oracle to SQL Server
2008
inj rav said
I
believe we need to include What is lazy initialization exception
Which was asked thrice to me in interview.
How to convert List of Integers to int array in
Ja
May 7, 2013 at 8:17 AM
10 Hibernate Interview Questions and Answers
for J
inj rav said
one more: what is N+1 problem and strategies to avoid this
Java Mistake 3 - Using "==" instead of equals()
to
May 7, 2013 at 8:24 AM
Java Program to Find Sum of Digits in a
Number usi
surajtamang said
► April (18)
Hi Javin,
► March (16)
found this Q&A "What does Session lock() method do in Hibernate?" not so clear. Can you please explain on
this matter little more?
I
► February (18)
► January (18)
May 7, 2013 at 10:18 PM
► 2012 (218)
Javin @ abstract class interface interview questions said
► 2011 (145)
@inj rav, Those are really good question, I have also seen lazy initialization exception couple of times, will
definitely include those.
► 2010 (33)
May 8, 2013 at 5:17 AM
References
Javin @ producer consumer solution BlockingQueue said
Java API documentation JDK 6
@Surajtamang, Apart from re-attaching a transient object to session, Session.lock(Object object,LockMode
lockMode) is also used to perform version check, they do this by acquiring an appropriate LockLevel e.g.
LockLevel.READ for version check. Different lock modes are provided to prevent entity from being read and
modified simultaneously from multiple source. By the way both overloaded version of lock()method is
deprecated and it's advised to use buildLockRequest(LockMode).lock(object) method for similar purpose.
Spring framework doc
Struts
ANT
Maven
May 8, 2013 at 5:28 AM
JDK 7 API
surajtamang said
MySQL
Thanks for the reply. Its more clear now.
Linux
Eclipse
May 8, 2013 at 11:40 PM
Copyright by Javin Paul 2012. Powered by Blogger.
Anonymous said
Hello, Can you please upload PDF version or let me know if there is any way to download these Hibernate
interview Questions as PDF? Thanks
May 9, 2013 at 1:04 AM
Anonymous said
In Hibernate how can we change the database only by just changing the hibernate.cfg.xml,if so how can we
handle migration issues.
May 9, 2013 at 7:31 PM
Anonymous said
It's JavaEE and for such a job interview I would assume questions about JPA and not the proprietary Hibernate
API
May 21, 2013 at 3:31 AM
Anonymous said
Save, saveorupdate, persist for me the only correct answer is: broken by design
May 21, 2013 at 4:14 AM
Anonymous said
Seriously - if presented with these questions and a huge domain model, using Hibernate as persistence, think
twice before applying for the job
May 21, 2013 at 4:52 AM

7/15/13

10 Hibernate InterviewQuestions and Answers for Java J2EE Programmers

Anonymous said Good Q&A. But, this is for legacy code. JPA is the king now.
Anonymous said
Good Q&A. But, this is for legacy code. JPA is the king now.
May 22, 2013 at 1:07 PM
Post a Comment
Enteryourcomment
Comment as:
Google Account
Publish
Preview
Newer Post
Home
Older Post
Subscribe to: Post Comments (Atom)
About Me
Privacy Policy