Pages

May 07, 2017

#Part 8: Hibernate Interview Questions

Should all the mapping files of hibernate have .hbm.xml extension to work properly?
No, having .hbm.xml extension is a convention and not a requirement for hibernate mapping file names. We can have any extension for these mapping files.

What is criteria API?
Criteria interface is used to create and execute queries which are criteria based. Such criteria-based queries are called object-oriented criteria queries. Criteria is a simple yet powerful API of hibernate which is used to retrieve entities through criteria object composition.

What are the benefits of using Hibernate template?
a. Session closing is automated.
b. Interaction with hibernate session is simplified.
c. Exception handling is automated.

How can we get hibernate statistics?
We can get hibernate statistics using getStatistics() method of SessionFactory class, eg: SessionFactory.getStatistics()

What different fetching strategies are of hibernate?
Join, Batch, Select and Sub-select Fetching

What’s difference between managed associations and hibernate associations?
Managed associations relate to container management persistence and are bi-directional while hibernate associations are unidirectional.

What are the drawbacks of using Hibernate Annotations?
  • Annotations are inclusive in the code and any changes made to the Annotations leads to code recompilation whereas an XML mapping file is placed outside the code file. So changes can be made without recompilation 
  • Using Annotations does not separate behavior and the POJO class. All information is in a single file. 
  • Some annotations can cause lifecycle mismatches and should be avoided. 
What is Cascading?
Entity relationships often depend on the existence of another entity. e.g Person–Address relationship. Without the Person, the Address entity doesn't have any meaning of its own. When we delete the Person entity, our Address entity should also get deleted.

JPA Cascade TypeAll JPA-specific cascade operations are represented by the javax.persistence.CascadeType enum containing entries: ALL, PERSIST, MERGE, REMOVE, REFRESH, DETACH.

Hibernate Cascade Type
Hibernate supports three additional Cascade Types along with those specified by JPA. These Hibernate-specific Cascade Types are available in org.hibernate.annotations.CascadeType: REPLICATE, SAVE_UPDATE, LOCK.
  • Cascade.ALL propagates all operations (including Hibernate-specific ones)from a parent to a child entity. Any change that happens to Person Entity must cascade to all associated entities (Address Entity in this case) also. This means that if you save a person into the database, then all associated address will also be saved into the database. If a Person is deleted, then all addresses associated with that Person will also be deleted.
  • CascadeType PERSIST propagates the persist operation from a parent to a child entity. When we save the Person entity, the Address entity will also get saved. FYI, the persist operation makes a transient instance persistent.
  • CascadeType.MERGE propagates the merge operation from a parent to a child entity. FYI, the merge operation copies the state of the given object onto the persistent object with the same identifier.
  • CascadeType.REMOVE propagates the remove operation from parent to child entity. Similar to JPA's CascadeType.REMOVE, we have CascadeType.DELETE, which is specific to Hibernate. There is no difference between the two.
  • When we use CascaseType.DETACH, the child entity will also get removed from the persistent context. FYI, the detach operation removes the entity from the persistent context. 
  • CascadeType.LOCK re-attaches the entity and its associated child entity with the persistent context again.
  • When we use this operation with CascadeType REFRESH, the child entity also gets reloaded from the database whenever the parent entity is refreshed. FYI, Refresh operations re-read the value of a given instance from the database. 
  • CascadeType.REPLICATE, a sync operation also propagates to child entities whenever performed on the parent entity. FYI, the replicate operation is used when we have more than one data source, and we want the data in sync.
  • CascadeType.SAVE_UPDATE propagates the same operation to the associated child entity. It's useful when we use Hibernate-specific operations like save, update, and saveOrUpdate.
-K Himaanshu Shuklaa..

No comments:

Post a Comment