Hibernate Interview Questions

What are the key benifits of Hibernate?

There are several benefits of using Hibernate 

  • Powerful object-oriented hibernate query language
  • Transparent persistence based on POJOs without byte code processing
  • Descriptive O/R Mapping through mapping file.
  • Automatic primary key generation 
  • Hibernate cache : Session Level, Query and Second level cache.
  • Performance: Lazy initialization, Outer join fetching, Batch fetching

What is hibernate session and session factory? How do you configure sessionfactory in spring configuration file?

Hibernate Session is the main runtime interface between a Java application and Hibernate. SessionFactory allows applications to create hibernate session by reading hibernate configurations file hibernate.cfg.xml.

    Configuration cfg = new Configuration().configure(); // Initialize the Hibernate environment
    SessionFactory factory = cfg.buildSessionFactory(); // Create the session factory
    Session session = factory.openSession(); // Obtain the new session object
 
The call to Configuration().configure() loads the hibernate.cfg.xml configuration file and initializes the Hibernate environment. Once the configuration is initialized, you can make any additional modifications you desire programmatically. However, you must make these modifications prior to creating the SessionFactory instance. An instance of SessionFactory is typically created once and used to create all sessions related to a given context. 
The main function of the Session is to offer create, read and delete operations for instances of mapped entity classes. Instances may exist in one of three states: 
  • transient: never persistent, not associated with any Session
  • persistent: associated with a unique Session
  • detached: previously persistent, not associated with any Session
A Hibernate Session object represents a single unit-of-work for a given data store and is opened by a SessionFactory instance. You must close Sessions when all work for a transaction is completed. The following illustrates a typical Hibernate session: 

    Session session = null; 
    UserInfo user = null; 
    Transaction tx = null;
    try {
       session = factory.openSession();
       tx = session.beginTransaction();
       user = (UserInfo)session.load(UserInfo.class, id);
       tx.commit();
    } catch(Exception e) {
       if (tx != null) {
          try {
             tx.rollback();
          } catch (HibernateException e1) {
             throw new DAOException(e1.toString()); }
       } throw new DAOException(e.toString());
    } finally {
       if (session != null) {
          try {
             session.close();
          } catch (HibernateException e) { }
       }
    }

What is the difference between hibernate get and load methods?

The following Hibernate code snippet retrieves a User object from the database: 

    User user = (User) session.get(User.class, userID); 

The get() method is special because the identifier uniquely identifies a single instance of a class. Hence it’s common for applications to use the identifier as a convenient handle to a persistent object. Retrieval by identifier can use the cache when retrieving an object, avoiding a database hit if the object is already cached. The get() method returns null if the object can’t be found.

Hibernate also provides a load() method:  

    User user = (User) session.load(User.class, userID); 

If load() can’t find the object in the cache or database, an exception is thrown. The load() method never returns null.  The load() method may return a proxy instead of a real persistent instance. A proxy is a placeholder instance of a runtime-generated subclass (through cglib or Javassist) of a mapped persistent class, it can initialize itself if any method is called that is not the mapped database identifier getter-method. 

On the other hand, get() never returns a proxy. Choosing between get() and load() is easy: If you’re certain the persistent object exists, and nonexistence would be considered exceptional, load() is a good option. If you aren’t certain there is a persistent instance with the given 
identifier, use get() and test the return value to see if it’s null. Using load() has a further implication: The application may retrieve a valid reference (a proxy) to a persistent instance without hitting the database to retrieve its persistent state. So load() might not throw an exception when it doesn’t find the persistent object in the cache or database; the exception would be thrown later, when the proxy is accessed.  

What type of transaction management is supported in hibernate? 

Hibernate communicates with the database via a JDBC Connection; hence it must support both managed and non-managed transactions.
non-managed in web containers:

    <bean id="transactionManager" class="org.springframework.orm.hibernate.HibernateTransactionManager">
        <property name="sessionFactory">
            <ref local="sessionFactory"/>
        </property>
    </bean>
 
managed in application server using JTA:

    <bean id="transactionManager" class="org.springframework.transaction.jta.JtaTransactionManager.">
        <property name="sessionFactory">
            <ref local="sessionFactory"/>
        </property>
    </bean>

What is lazy loading and how do you achieve that in hibernate?

Lazy setting decides whether to load child objects while loading the Parent Object. You need to specify parent class.Lazy = true in hibernate mapping file. By default the lazy loading of the child objects is true. This make sure that the child objects are not loaded unless they are explicitly invoked in the application by calling getChild() method on parent. In this case hibernate issues a fresh database call to load the child when getChild() is actully called on the Parent object. But in some cases you do need to load the child objects when parent is loaded. Just make the lazy=false and hibernate will load the child when parent is loaded from the database. 

Examples: Address child of User class can be made lazy if it is not required frequently. But you may need to load the Author object for Book parent whenever you deal with the book for online bookshop. 

Hibernate does not support lazy initialization for detached objects. Access to a lazy association outside of the context of an open Hibernate session will result in an exception. 

What are the different fetching strategy in Hibernate?

Hibernate3 defines the following fetching strategies:
  • Join fetching - Hibernate retrieves the associated instance or collection in the same SELECT, using an OUTER JOIN.
  • Select fetching - a second SELECT is used to retrieve the associated entity or collection. Unless you explicitly disable lazy fetching by specifying lazy="false", this second select will only be executed when you actually access the association.
  • Subselect fetching - a second SELECT is used to retrieve the associated collections for all entities retrieved in a previous query or fetch. Unless you explicitly disable lazy fetching by specifying lazy="false", this second select will only be executed when you actually access the association.Batch fetching - an optimization strategy for select fetching - Hibernate retrieves a batch of entity instances or collections in a single SELECT, by specifying a list of primary keys or foreign keys.
For more details read short primer on fetching strategy at http://www.hibernate.org/315.html

What are different types of cache hibernate supports ?

  1. Caching is widely used for optimizing database applications. Hibernate uses two different caches for objects: 
  2. First-level cache: First-level cache is associated with the Session objectBy default, Hibernate uses first-level cache on a per-transaction basis. Hibernate uses this cache mainly to reduce the number of SQL queries it needs to generate within a given transaction. For example, if an object is modified several times within the same transaction, Hibernate will generate only one SQL UPDATE statement at the end of the transaction, containing all the modifications.
  3. Second-level cache: second-level cache is associated with the Session Factory object. To reduce database traffic, second-level cache keeps loaded objects at the Session Factory level between transactions. These objects are available to the whole application, not just to the user running the query. This way, each time a query returns an object that is already loaded in the cache, one or more database transactions potentially are avoided. 
  4. Query-level cache: In addition, you can use a query-level cache if you need to cache actual query results, rather than just persistent objects. The query cache should always be used in conjunction with the second-level cache. Hibernate supports the following open-source cache implementations out-of-the-box: 
    • EHCache is a fast, lightweight, and easy-to-use in-process cache. It supports read-only and read/write caching, and memory- and disk-based caching. However, it does not support clustering.
    • OSCache is another open-source caching solution. It is part of a larger package, which also provides caching functionalities for JSP pages or arbitrary objects. It is a powerful and flexible package, which, like EHCache, supports read-only and read/write caching, and memory- and disk-based caching. It also provides basic support for clustering via either JavaGroups or JMS.
    • SwarmCache is a simple cluster-based caching solution based on JavaGroups. It supports read-only or nonstrict read/write caching (the next section explains this term). This type of cache is appropriate for applications that typically have many more read operations than write operations.
    • JBoss TreeCache is a powerful replicated (synchronous or asynchronous) and transactional cache. Use this solution if you really need a true transaction-capable caching architecture.
    • Commercial Tangosol Coherence cache.

What are the different caching strategies?

  • Read-only: This strategy is useful for data that is read frequently but never updated. This is by far the simplest and best-performing cache strategy.
  • Read/write: Read/write caches may be appropriate if your data needs to be updated. They carry more overhead than read-only caches. In non-JTA environments, each transaction should be completed when Session.close() or Session.disconnect() is called.
  • Nonstrict read/write: This strategy does not guarantee that two transactions won't simultaneously modify the same data. Therefore, it may be most appropriate for data that is read often but only occasionally modified.
  • Transactional: This is a fully transactional cache that may be used only in a JTA environment.

How do you configure 2nd level cach in hibernate?

To activate second-level caching, you need to define the hibernate.cache.provider_class property in the hibernate.cfg.xml file as follows: 

    <hibernate-configuration>
        <session-factory>
            <property name="hibernate.cache.provider_class">org.hibernate.cache.EHCacheProvider</property>
        </session-factory>
    </hibernate-configuration>

By default, the second-level cache is activated and uses the EHCache provider.
To use the query cache you must first enable it by setting the property hibernate.cache.use_query_cache to true in hibernate.properties.

What is the difference between sorted and ordered collection in hibernate?

A sorted collection is sorted in-memory using java comparator, while order collection is ordered at the database level using order by clause.

What are the types of inheritence models and describe how they work like vertical inheritence and horizontal?

There are three types of inheritance mapping in hibernate :
Example: Let us take the simple example of 3 java classes. Class Manager and Worker are inherited from Employee Abstract class.
  1. Table per concrete class with unions : In this case there will be 2 tables. Tables: Manager, Worker [all common attributes will be duplicated]
  2. Table per class hierarchy: Single Table can be mapped to a class hierarchy. There will be only one table in database called 'Employee' that will represent all the attributes required for all 3 classes. But it needs some discriminating column to differentiate between Manager and worker;
  3. Table per subclass: In this case there will be 3 tables represent Employee, Manager and Worker

What is the difference between the session.get() method and the session.load() method? 

Both the session.get(..) and session.load() methods create a persistent object by loading the required object from the database. But if there was not such object in the database then the method session.load(..) throws an exception whereas session.get(&) returns null. 

What is the difference between the session.update() method and the session.lock() method? 

Both of these methods and saveOrUpdate() method are intended for reattaching a detached object. The session.lock() method simply reattaches the object to the session without checking or updating the database on the assumption that the database in sync with the detached object. It is the best practice to use either session.update(..) or session.saveOrUpdate(). Use session.lock() only if you are absolutely sure that the detached object is in sync with your detached object or if it does not matter because you will be overwriting all the columns that would have changed later on within the same transaction. 

Note: When you reattach detached objects you need to make sure that the dependent objects are reatched as well. 

How would you reatach detached objects to a session when the same object has already been loaded into the session? 

You can use the session.merge() method call. 

What are the general considerations or best practices for defining your Hibernate persistent classes? 

  1. You must have a default no-argument constructor for your persistent classes and there should be getXXX() (i.e accessor/getter) and setXXX( i.e. mutator/setter) methods for all your persistable instance variables. 
  2. You should implement the equals() and hashCode() methods based on your business key and it is important not to use the id field in your equals() and hashCode() definition if the id field is a surrogate key (i.e. Hibernate managed identifier). This is because the Hibernate only generates and sets the field when saving the object. 
  3. It is recommended to implement the Serializable interface. This is potentially useful if you want to migrate around a multi-processor cluster. 
  4. The persistent class should not be final because if it is final then lazy loading cannot be used by creating proxy objects. 
  5. Use XDoclet tags for generating your *.hbm.xml files or Annotations (JDK 1.5 onwards), which are less verbose than *.hbm.xml files. 
Comments