Thursday, 10 October 2013

Hibernate Interview Questions Part - 2



Interview Questions

Questions: - What is the use of inverse in Hibernate?

Answer: - The “inverse” keyword is always declare in one-to-many and many-to-many relationship. We 
don’t have “inverse” keyword in many-to-one relationship. “inverse” is used to decide which side is the relationship owner will manage the relationship (insert or update of the foreign key column). It means which side is responsible to take care of the relationship.

inverse = “true” describes that this is the relationship owner, where as inverse=”false” (default) means it’s not the relationship owner.

Questions: - What is the use of cascade in Hibernate?

Answer: -

Cascade values are
1.      none (default)
2.      save
3.      update
4.      save-update
5.      delete
6.      all
7.      all-delete-orphan

If cascade = “none” and if we execute save (or update or delete) operation on parent class object, then child class objects will not be effected.

If we write cascade = “all”, then the operations like save or delete or update executed at parent class object will be effected to child class object also.

If we write cascade = “save-update”, then the operations like save and update executed at parent class object will also be effected to child class object also.

In an application, if a child record is removed from the collection and if we want to remove that child record immediately from the database, then we need to set the cascade =”all-delete-orphan”


Questions: - What is the use of BAG collection in hibernate?

Answer: - If our table does not have an index column, and we want to use collection property type, then in this case we can use <bag> Collection property in Hibernate. When a collection of data is retrieved and assigned to bag collection then bag does not retain its order but it can be optionally sorted or ordered. A bag permit duplicates it means it does not have primary key. So finally we can tell that a bag is an unordered, unkeyed collection that can contain the same element multiple times.


Questions: - What is optimistic locking in Hibernate?
Answer: - When we load object in one transaction, modify the data and save it later in another transaction then in this situation Hibernate optimistic locking works. This locking ensures that some other transaction hasn’t changed that same object in the database in between. However, optimistic locking doesn't affect isolation of concurrent transactions.  


Questions: - What is transactional write behind in Hibernate?

Answer: - When we call session.save(Object) in the hibernate code then it does not fire the SQL insert query immediately. Similarly when we call session.delete(Object) or session.update(Object)  then it will not fire the corresponding SQL queries(delete and update queries) immediately.

The meaning is thatwhen objects associated with persistence context are modified, the changes are not immediately propagated to the database.

Hibernate collects all such database operations associated with a transaction and create minimum set of SQL queries and execute them. This will provide us 2 advantages:-

  • In case of multiple updates or inserts or deletes, Hibernate is able to make use of the JDBC Batch API to optimize performance.
  • Every property change in the object does not cause a separate sql update query to be executed.
  • Avoiding unwanted SQL queries ensures minimum hits to database thus reducing the network latency.

This delayed execution of sql queries is known as transactional write behind.

No comments:

Post a Comment