What are the two locking strategies in Hibernate?

There are two popular locking strategies that can be used in Hibernate:

  • Optimistic: In Optimistic locking we assume that multiple transactions can complete without affecting each other. So we let the transactions do their work without locking the resources initially.Just before the commit, we check if any of the resource has changed by another transaction, then we throw exception and rollback the transaction.
  • Pessimistic: In Pessimistic locking we assume that concurrent transactions will conflict while working with same resources. So a transaction has to first obtain lock on the resources it wants to update.The other transaction can proceed with same resource only after the lock has been released by previous transaction.

What is the use of version number in Hibernate?

Version number is used in optimistic locking in Hibernate. When a transaction modifies an object, it increments its version. Based on version number, second transaction can determine if the object it has read earlier has changed or not.

If the version number at the time of write is different than the version number at the time of read, then we should not commit the transaction.

What is the use of session.lock() method in Hibernate?

Session.lock() is a deprecated method in Hibernate. We should not use it.

Instead we should call buildLockRequest(LockMode).lock(entityName, object) method in Hibernate.

What inheritance mapping strategies are supported by Hibernate?

Hibernate supports following inheritance mapping strategies between classes and tables:

  • Table per class hierarchy: In case of multiple types of books, we can have one book class and one book table. We can store all child classes of book like- HardCoverBook, PaperBackBook etc in same table book. But we can identify the subclasses by a BookType column in Book table.
  • Table per subclass: In this case we can have separate table for each kind of book. HardCoverBook table for HardCoverBook book class. PaperBackBook table for PaperBackBook book class. And there will be a parent table, Book for Book class.
  • Table per concrete class: In this case also we have separate table for each kind of book. But in this case we have even inherited properties defined inside each table. There is no parent table Book for Book class, since it is not a concrete class.

When should we use get() method or load() method in Hibernate?

As a thumb rule we can follow these guidelines:

<li>We should use get() method when we want to load an object.</li>

<li>We should use load() method when we need a reference to an object without running extra SQL queries.</li>

What is a derived property in Hibernate?

In Hibernate, a derived property is not mapped to any column of a database table.

A derived property is computed at runtime by evaluation of an expression.

These are read only properties.

Egg. In this example profitMargin is derived from salePrice and buyPrice.


How can we use Named Query in Hibernate?

A Named SQL query is the HQL query that is associated with a string name and can be referenced in the application by name.

It can be used in following ways:

  • XML Mapping File: We can define it in XML mapping file.Egg.
  • Annotation: We can also mark Named SQL with annotation.@NamedQueries({
    name = “findBookByAuthor”,
    query = “from Book s where s.author = :author”

How can we check is a collection is initialized or not under Lazy Initialization strategy?

Hibernate provides two convenient methods, Hibernate.initialize() and Hibernate.isInitialized() to check whether a collection is initialized or not.

By using Hibernate.initialize() we can force the initialization of a collection in Hibernate.

What are the different strategies for cache mapping in Hibernate?

Hibernate provides following strategies for cache mapping:

  • Read only: If an application requires caching only for read but not for write operations, then we can use this strategy. It is very simple to use and give very good performance benefit.It is also safe to use in a cluster environment.
  • Read/Write: If an application also needs caching for write operations, then we use Read/Write strategy.Read/write cache strategy should not be used if there is requirement for serializable transaction isolation level.

    If we want to use it in a cluster environment, we need to implement locking mechanism.

  • Nonstrict Read/Write: If an application only occasionally updates the data, then we can use this strategy. It cannot be used in systems with serializable transaction isolation level requirement.
  • Transactional: This strategy supports full transactional cache providers like JBoss TreeCache.

What is the difference between a Set and a Bag in Hibernate?

A Bag in Hibernate is an unordered collection. It can have duplicate elements. When we persist an object in a bag, there is no guarantee that bag will maintain any order.

A Set in Hibernate can only store unique objects. If we add the same element to set second time, it just replaces the old one. By default a Set is unordered collection in Hibernate.