Crucial Hibernate Interview Questions


Q11: Cite the main differences between openSession and getCurrentSession?

Ans: This getCurrentSession() method allows the session bound to come back to the context. If you want it to function properly, you need to configure it in Hibernate configuration file. The session object is possessed by the context of Hibernate. You don’t need to close it as it closes with the SessionFactory.

  • openSession forms new sessions objects. On the other hand, getCurrentSession forms a new Session if is not already present. Otherwise, it will use the same session.
  • In openSession, you have to remove and close session objects. The other doesn’t need flushing and closing of the objects. Hibernate handles this internally without bothering you.
  • When you are working in a single thread environment, openSession has slower performance than getCurrentSession.
  • To call openSession, you do not need to configure any property. But, for the other session, you need configuration.

Q12: What is Hibernate Proxy and how does it help in Lazy loading?

Ans:To provide support to Lazy loading, Hibernate makes use of a proxy object. When you request to load an object, it is transferred from the database. It comes with a reference to another concrete object, so instead of that concrete associated object, Hibernate returns a proxy.

  • If you want to load data from tables, you won’t be able to load every single mapped object.
  • Using the getter method, once you reference a child object, the proxy code will be integrated into the database and it will end up loading the linked object. Note that, in this situation, the linked entity must not exist in the session cache.
  • By using Java assist, it creates sub-classed implementations of your entity objects with efficacy.

Q13: When do you use merge() and update() in Hibernate?

Ans: Most of the developers who take this test find this question tough. Even though the answer to it is very straightforward and consists of a few lines, it may put you in a tricky situation. To save you from being clueless in your interview, here is when to use the merge and update in hibernate.

Update(): You use it when you are certain that the Hibernate Session does not have a persistent instance already with the same id.

merge(): Without knowing about the state of the Session, you can use it to merge your modifications whenever you want.

Q14: Cite the fundamental difference between get() vs load() method in Hibernate?

Ans: This is undoubtedly one of the most asked questions during the Hibernate Interview. That is why we only tell you the primary difference between the get() and load() methods.

load(): Suppose, an object is being passed to them has an unidentified ID, it will throw an exception.

get(): is going to return null.

load(): It can easily return the proxy without going to the database if it isn’t necessary.

get(): It must hit the database.

Thus, in some cases, load() can be more rapid as compared to the get() method.

Q15: Tell the difference between the transient, persistent, and detached state in Hibernate?

Ans: In the Transient state, in the Java program, the formation of new objects takes place. They are not related to any Hibernate Session. You may have guessed the object that is involved in this state. In the Persistent state, the object is associated with a Hibernate session. This object is famous as the Persistent object.

The object that has prior associated with Hibernate session, but now it’s not associated is called a detached object. If you want to store these objects in the database, you can call the save() or persist() method and change them into the Persistent state.

In the Detached state, you can call either update() or saveOrUpdate() method and attach the object again to Hibernate sessions.