Wednesday, October 17, 2012

Hibernate Q&A

Spring Support

UserDAOHibernate extends org.springframework.orm.hibernate.support.HibernateDaoSupport


    <bean id="userDAO" class="org.appfuse.dao.hibernate.UserDAOHibernate">
        <property name="sessionFactory"><ref local="sessionFactory"/></property>
    </bean>    


    <!-- Hibernate SessionFactory -->
    <bean id="sessionFactory" class="org.springframework.orm.hibernate.LocalSessionFactoryBean">
        <property name="dataSource"><ref local="dataSource"/></property>
        <property name="mappingResources">
            <list>
                <value>org/appfuse/model/User.hbm.xml</value>
            </list>
        </property>
        <property name="hibernateProperties">
        <props>
            <prop key="hibernate.dialect">net.sf.hibernate.dialect.HSQLDialect</prop>
            <prop key="hibernate.hbm2ddl.auto">update</prop>
        </props>
        </property>
    </bean>


    <!-- Transaction manager for a single Hibernate SessionFactory (alternative to JTA) -->
    <bean id="transactionManager" class="org.springframework.orm.hibernate.HibernateTransactionManager">
        <property name="sessionFactory"><ref local="sessionFactory"/></property>
    </bean>
    <bean id="userManagerTarget" class="org.appfuse.service.impl.UserManagerImpl">
        <property name="userDAO"><ref local="userDAO"/></property>
    </bean>

    <bean id="userManager" 
        class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
        <property name="transactionManager"><ref local="transactionManager"/></property>
        <property name="target"><ref local="userManagerTarget"/></property>
        <property name="transactionAttributes">
            <props>
                <prop key="save*">PROPAGATION_REQUIRED</prop>
                <prop key="remove*">PROPAGATION_REQUIRED</prop>
                <prop key="*">PROPAGATION_REQUIRED,readOnly</prop>
            </props>
        </property>
    </bean>

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 type of transaction management is supported in hibernate? 

Non-Managed transaction:

     <bean id="transactionManager" class="org.springframework.orm.hibernate.HibernateTransactionManager">
        <property name="sessionFactory">
            <ref local="sessionFactory"/>
        </property>
    </bean>

Managed transaction 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.


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 ?

Caching is widely used for optimizing database applications. Hibernate uses two different caches for objects:
  1. First-level cache: First-level cache is associated with the Session object. By 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.
  2. 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. 
  3. 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. 

No comments:

Post a Comment