Pages

May 06, 2017

#Part 6: Hibernate Interview Questions

What is the purpose of Session.beginTransaction()?
Hibernate keeps a log of every data exchange with the help of a transaction. Thereon, in case a new exchange of date is about to get initiated, the function Session.beginTransaction is executed in order to begin the transaction.

What are the ways to express joins in HQL?
HQL allows you to express joins in four ways:
• An implicit association join
• A fetch join in the FROM clause
• A theta-style join in the WHERE clause
• An ordinary join in the FROM clause

Explain the different ways Hibernate manages concurrency?
Hibernate has numerous ways of managing concurrency. They are as listed below:
• Automatic versioning
• Detached object
• Extended user sessions

What is named SQL query in Hibernate?
Named queries are SQL queries which are defined in mapping document using < sql-query > tag and called using Session.getNamedQuery() method. Named query allows us to refer a particular query by the name which we had provided. We can define named query in hibernate either by using annotations or XML mapping file. @NameQuery is used to define single named query and @NameQueries is used to define multiple named query in hibernate.

What is the difference between sorted and ordered collection in hibernate?
A sorted collection is sorted in memory by using Java Comparator while an ordered collection uses database's order by clause for ordering. For large data set it's better to use ordered collection to avoid any OutOfMemoryError in Java, by trying to sort them in memory.

What does Session lock() method do in Hibernate?
Session's lock() method reattach object without synchronizing or updating with the database. So you need to be very careful while using lock() method. We can always use Session's update() method to sync with the database during reattachment.

Why it's important to provide no argument constructor in Hibernate Entities?
Every Hibernate Entity class must contain a no argument constructor, because Hibernate framework creates an instance of them using Reflection API, by calling Class.newInstance() method. This method will throw InstantiationException if it doesn't found any argument constructor inside Entity class.

What is the requirement for a Java object to become a Hibernate entity object?
Prefer non final class and must provide a default, no-argument constructor.

Can we make a Hibernate Entity Class final? Why JPA Entity or Hibernate persistence class should not be final?
Yes, you can make a Hibernate Entity class final, but that's not a good practice. Since Hibernate uses a proxy pattern for performance improvement in the case of the lazy association, by making an entity final, Hibernate will no longer be able to use a proxy, because Java doesn't allow extension of the final class, thus limiting your performance improvement options. Though, you can avoid this penalty if your persistent class is an implementation of an interface, which declares all public methods defined in the Entity class.

Important points to remember:
1). Hibernate doesn't create a proxy for final class, instead, they use the real class.
2). But Hibernate does create a proxy for a non-final class with final methods. In Java we cannot override final methods, that's why the code inside final method remain unchanged in the proxy class. When we call a final method on a proxy, it will simply delegate the call to the super class method. We should be careful not to modify state on final method because calling them on proxy has a good chance of throwing NullPointerException, as the object was likely to be not initialized at that point. That's the reason we should avoid final methods on hibernate entity class, until and unless we know what you are doing.
3). As per Hibernate documentation, if you're going to make a class final you should explicitly disable proxy generation by adding @Proxy(lazy=false), however there is no difference in doing this or by just making the class final.
4). If you still want to make the Hibernate entity classes final then add an interface, which your entity is going to implement. Declares all of its public methods in the interface. This will still allow Hibernate to use proxies.

Explain the difference between Hibernate and Spring.
Hibernate is an ORM tool for data persistency. Spring is a framework for enterprise applications. Spring supports hibernate and provides the different classes which are templates that contains the common code.

Explain the main difference between Entity Beans and Hibernate.
Entity beans are to be implemented by containers, classes, descriptors. Hibernate is just a tool that quickly persist the object tree to a class hierarchy in a database and without using a single SQL statement. The inheritance and polymorphism is quite simply implemented in hibernate which is out of the box of EJB and a big drawback.
-K Himaanshu Shuklaa..

No comments:

Post a Comment