Sponsored Links

Ad by Google
Hibernate is still on demand and very popular ORM framework. In every Java,J2EE interview, you will faced at least 2-3 Hibernate related interview question. So, I thought I have to list popular Hibernate interview questions in my post after completion of Hibernate Tutorial now time has came to list very popular or you can say FAQ Hibernate Questions/answer here. Although I have posted core java specific 20 core java interview question and 10 JSP Servlet interview question

So start with most respected Hibernate Interview question,
What is difference between load() and get() method of Hibernate
This question is just to check your working knowledge on Hibernate and once the interviewer got expected answer from you than start digging into the the deep of Hibernate. The Hibernate load() and get() both methods are used to retrieve the records from the database, I have listed the differences in separately so for complete differences, read the very good explanation at hibernate load vs get
How Second Level Cache works in Hibernate
This is very popular Hibernate interview question just like, in core java every one asked Why String is immutable in java? almost every interviewer asked you this question to see your actual working knowledge of some advance feature of Hibernate and only those candidate gets the complete answer who actually implemented Second Level Cache in Hibernate.
In Hibernate second level cache means, JVM level cache or you can say session factory level cache. The second level cache is used to improve the performance of application by reducing the database hit. By default second level cache is not configured, to use second level cache you have to enable the second level cache with cache providers. Once you enabled the second level cache successfully, It will work for you. After successfully enabling the second level cache, If you request for an entity, It will first search the entity into the first level cache(Session), If not found in the first level cache than It will search in second level cache and return the entity from the second level cache if not found in the second level, than It will hit database and load the entity into second level cache and first level cache and return from there. Must read specific post on How Second level cache works in Hibernate

What are the core classes and interfaces of Hibernate
Another very popular and common Hibernate interview question. If you are realy used Hibernate than you must know the main classes and interfaces of Hibernate, although Hibernate has thousands of classes and interfaces. But interviewer wants to know the core/base classes and interfaces and those are -
  1. Configuration (class)
  2. ServiceRegistry (interface)
  3. SessionFactory (interface)
  4. Session (interface)
  5. Transaction (interface)
  6. Query (interface)
  7. Criteria(interface)
Just listing the above classes and interfaces is not enough you must have to know the details of each one because once you list, than interviewer ask you about few of them in details, so be ready, here I have listed in details of each other core classes and interface of Hibernate must read.
What is difference between SessionFactory and Session
Although this question does not make many more sense for me, and yes smart interviewer almost never asked this question. Because asking the differences between Mercedes an Mercedes SUV is totally different, Mercedes who manufactured Mercedes SUV, Benz etc. So you can ask about differences between different models of Mercedes like Mercedes SUV vs Mercedes Benz, same way in Hibernate you can ask about difference between open session and current session.
OK let's leave the conversation and see the differences -
The SessionFactory is an interface available inside org.hibernate.SessionFactory and as it's name implies it's a factory of session, SessionFactory is a immutable cache of compiled mappings for a single database and it's instance is thread safe. In short Session Factory is all about session, you can get the session instances from the SessionFactory only.
Whereas Session, it's again an interface, Session is a heart of hibernate because to perform any operation with entity in hibernate you must need session. It's a single threaded short lived object, which represent conversational state between application and database. Or you can say, it's a run time interface between your application and hibernate, session can be obtain from the session factory.

What are the fetching strategies supported by Hibernate
Hibernate is very popular ORM framework and it's fetching strategies are one of the powerful feature of Hibernate. Fetching strategies are always helps while optimization you Hibernate Query.
There are four different types of fetching strategies supported by Hibernate and those are -
  1. Join fetching (@Fetch(FetchMode.JOIN))
  2. Subselect fetching (@Fetch(FetchMode.SUBSELECT))
  3. Batch fetching (@BatchSize(size=x))
  4. Select fetching (@Fetch(FetchMode.SELECT))
If you have enough time than you must see how they works, Hibernate Fetching Strategies, which will help you to discuss them smartly if required in your interview.

Can you write a syntax to map One-To-Many association
Another very popular Hibernate interview question, This will help interviewer to judge your practical experienced and working knowledge over hibernate, so almost every interviewer must asked this question and to answer this question you must have a practical experiencing over association mapping in Hibernate. I am copying the syntax from my One-to-Many association mapping  post of book and author relationship, Author 1 ---------* Books.
private Set<Book> books = new HashSet<>();

@JoinColumn(name = "author_id", nullable=false)
private Author author;

How many object states in Hibernate
There are three different states of hibernate object and those are listed below -
  1. transient
  2. persistent
  3. detached
For more in details, you can read  Object states in Hibernate

How to create custom identifier or generator class in Hibernate
To store object into the database you must need at least one identifier to be defined into your mapping entity class. Hibernate has broader range of built in generator classes but sometimes you need to create your own generator class and interviewer wants to know from you if required how to create them.
Step 1.  Create a class
Step 2.  Implement org.hibernate.id.IdentifierGenerator interface
Step 3.  Provide the implementation of generate method
public Serializable generate(SessionImplementor session, Object arg1)throws HibernateException {}

Here is a complete example of using custom generator class How to use custom generator class

What is difference between save and persist method
Differences :

  1. Return type of save() is Serializable. Here is complete signature of save() method, Serializable save(Object object) throws HibernateException. Whereas return type of persist() is void. Here is complete signature of persist() method, void persist(Object object) throws HibernateException
  2. The persist() makes a transient instance persistent. However, it does not guarantee that the identifier value will be assigned to the persistent instance immediately, the assignment might happen at flush time. Whereas save() makes a transient instance persistent, and does guarantee to return an identifier. If an INSERT has to be executed to get the identifier ( e.g. "identity" generator, not "sequence"), this INSERT happens immediately.
  3. The persist() also guarantees that it will not execute an INSERT statement if it is called outside of transaction boundaries. This is useful in long-running conversations with an extended Session/persistence context. Whereas save() INSERT happens immediately, no matter if you are inside or outside of a transaction. This is problematic in a long-running conversation with an extended Session/persistence context.

How to enabled second level cache in Hibernate
To enabled second level cache, you need to define cache provider first here is the list of cache providers, than you can enable it by using below line of syntax-
<property name="hibernate.cache.use_second_level_cache">true</property>
But to use the second level cache you need to add cache providers jars,configuration etc, here is a complete step by step implementation of Second Level Cache, please see at least once before going for an interview. It's very simple and easy to understand.

Sponsored Links


Post a Comment