Core Methods

Understanding the usage of core Hibernate methods like get(), load(), save(), persist(), update(), merge(), evict(), remove(), etc.

Core Methods Interview with follow-up questions

Interview Question Index

Question 1: Can you explain the difference between the get() and load() methods in Hibernate?

Answer:

The get() and load() methods are used to retrieve an object from the database in Hibernate.

The get() method is used to immediately retrieve the object from the database. If the object with the provided identifier does not exist in the database, the get() method returns null.

On the other hand, the load() method returns a proxy object without hitting the database immediately. The actual database query is executed only when a method is called on the proxy object. If the object with the provided identifier does not exist in the database, the load() method throws an exception.

In summary, the main difference between get() and load() is that get() returns null if the object does not exist, while load() throws an exception.

Back to Top ↑

Follow up 1: What is the return type of these methods?

Answer:

The get() method returns the object from the database or null if the object does not exist.

The load() method returns a proxy object that represents the requested object. The actual object is loaded from the database when a method is called on the proxy object.

Back to Top ↑

Follow up 2: In what scenarios would you prefer to use get() over load() and vice versa?

Answer:

You would prefer to use get() over load() when you want to immediately retrieve the object from the database and handle the case when the object does not exist by checking for null.

On the other hand, you would prefer to use load() over get() when you are confident that the object with the provided identifier exists in the database and you want to avoid the immediate database query. However, you need to be cautious when using load() as it throws an exception if the object does not exist.

Back to Top ↑

Follow up 3: What happens when there is no record with the provided identifier in the database?

Answer:

If there is no record with the provided identifier in the database:

  • The get() method returns null.
  • The load() method throws an exception.
Back to Top ↑

Question 2: What is the purpose of the save() method in Hibernate?

Answer:

The save() method in Hibernate is used to save an object into the database. It is used to insert a new record if the object is not already persistent, or update the existing record if the object is already persistent.

Back to Top ↑

Follow up 1: What is the return type of the save() method?

Answer:

The return type of the save() method in Hibernate is Serializable. It returns the identifier (primary key) of the saved object.

Back to Top ↑

Follow up 2: What happens if we call the save() method without setting the primary key?

Answer:

If we call the save() method without setting the primary key, Hibernate will generate a primary key value for the object automatically. This is typically done using an identifier generator strategy configured in the Hibernate mapping.

Back to Top ↑

Follow up 3: Can you explain the difference between save() and persist() methods?

Answer:

Both save() and persist() methods in Hibernate are used to save an object into the database. However, there are some differences:

  • The save() method returns the identifier (primary key) of the saved object, while the persist() method does not return anything.
  • The save() method can be called before or after the transaction is committed, while the persist() method can only be called within an active transaction.
  • The save() method generates a new identifier value for a transient object, while the persist() method throws an exception if called on a transient object with a non-null identifier.
Back to Top ↑

Question 3: How does the update() method work in Hibernate?

Answer:

The update() method in Hibernate is used to update the state of a persistent object. It takes an object as a parameter and updates the corresponding row in the database table with the new values from the object. The update() method is typically used when we want to modify the state of an object and persist the changes to the database.

Back to Top ↑

Follow up 1: What happens if we call update() on a transient object?

Answer:

If we call update() on a transient object, Hibernate will throw an exception. A transient object is an object that is not associated with any database row. In order to update an object, it must be in the persistent state, meaning it must be associated with a database row. To update a transient object, we need to first make it persistent by either saving it using the save() method or retrieving it using the get() or load() methods.

Back to Top ↑

Follow up 2: What is the difference between update() and merge() methods?

Answer:

The update() and merge() methods in Hibernate are used to update the state of persistent objects, but they have some differences:

  • The update() method is used to update the state of a persistent object with the new values from the object passed as a parameter. It throws an exception if the object is transient.

  • The merge() method is used to merge the state of a detached object with the state of a persistent object. It returns the persistent object with the merged state. If the object is transient, it will be saved as a new row in the database.

In summary, the update() method updates the state of a persistent object in place, while the merge() method merges the state of a detached object with a persistent object and returns the merged object.

Back to Top ↑

Follow up 3: Can you explain the scenario where update() method can lead to an exception?

Answer:

The update() method in Hibernate can lead to an exception in the following scenario:

  • If we call update() on a transient object, Hibernate will throw a TransientObjectException. A transient object is an object that is not associated with any database row. In order to update an object, it must be in the persistent state, meaning it must be associated with a database row. To update a transient object, we need to first make it persistent by either saving it using the save() method or retrieving it using the get() or load() methods.
Back to Top ↑

Question 4: What is the role of the evict() method in Hibernate?

Answer:

The evict() method in Hibernate is used to remove a persistent instance from the session cache. It detaches the object from the session, which means any further changes made to the object will not be tracked by Hibernate. The evict() method is typically used to release memory and improve performance by removing unnecessary objects from the session cache.

Back to Top ↑

Follow up 1: What happens to the state of the object after evict() method is called?

Answer:

After the evict() method is called, the object becomes detached from the session. This means that any changes made to the object will not be automatically synchronized with the database. The object will no longer be managed by Hibernate and any further operations on the object will not be tracked by Hibernate.

Back to Top ↑

Follow up 2: Can you explain the difference between evict() and remove() methods?

Answer:

The evict() method in Hibernate removes a persistent instance from the session cache and detaches it from the session. It does not delete the object from the database.

On the other hand, the remove() method is used to delete an object from the database. It not only removes the object from the session cache but also deletes it from the database.

In summary, evict() removes the object from the session cache without deleting it from the database, while remove() deletes the object from both the session cache and the database.

Back to Top ↑

Follow up 3: What is the use case for the evict() method?

Answer:

The evict() method in Hibernate is useful in scenarios where you want to remove an object from the session cache without deleting it from the database. This can be beneficial in situations where you have a large number of objects in the session cache and you want to release memory by removing unnecessary objects. It can also be used when you want to detach an object from the session to prevent any further changes from being tracked by Hibernate.

Back to Top ↑

Question 5: Can you explain the purpose of the remove() method in Hibernate?

Answer:

The remove() method in Hibernate is used to delete an object from the database. It marks the object for deletion and removes it from the session cache. The actual deletion from the database will occur when the session is flushed or when a transaction is committed.

Back to Top ↑

Follow up 1: What is the state of the object after remove() method is called?

Answer:

After the remove() method is called, the object becomes detached from the Hibernate session. It is no longer associated with any persistent context and any further changes made to the object will not be synchronized with the database.

Back to Top ↑

Follow up 2: What happens if we call remove() on a detached object?

Answer:

If we call remove() on a detached object, Hibernate will throw a NonUniqueObjectException. This exception is thrown because Hibernate cannot determine which persistent instance of the object to delete from the database.

Back to Top ↑

Follow up 3: Can you explain the difference between remove() and delete() methods?

Answer:

The remove() method is a Hibernate-specific method that marks an object for deletion and removes it from the session cache. The actual deletion from the database occurs when the session is flushed or when a transaction is committed.

On the other hand, the delete() method is a JPA standard method that directly deletes an object from the database. It does not remove the object from the session cache and does not require a session flush or transaction commit to perform the deletion.

Back to Top ↑