Butter Dev Logo
Search:   

December 24, 2008

DWR and Hibernate 3.x

Recently there have been some questions on DWR’s user mailing list about integration with Hibernate.  Most of the questions stem from a misunderstanding of how DWR works and I thought it would be helpful to write a post to give new users some information on what to expect from DWR.

DWR provides two main integration points with Hibernate:

  1. A converter (hibernate3) that attempts to avoid reading from un-initialized (lazy) properties.
  2. A filter (H3SessionAjaxFilter) which provides a Hibernate Session to the hibernate3 converter if un-initialized properties need to be read.

What converter should I use and how do I configure it?

You should use the hibernate3 converter if:

  1. The returned object has lazy properties which have not been excluded in your DWR configuration and you do not want DWR to attempt to serialize these properties.
    1. Add a converter to your dwr.xml:
      <allow> 
        .... 
        <convert converter="hibernate3" match="yourpackage.yourClass" />
        ....
      </allow>
      
  2. The returned object has lazy properties and you want DWR to attempt to serialize these properties. 
    1. Add a converter to your dwr.xml with the assumeSession parameter set to true.  This will tell DWR it should attempt to access the Hibernate session to read un-initialized properties:
      <allow> 
        .... 
        <convert converter="hibernate3" match="yourpackage.yourClass">
          <param name="assumeSession" value="true"/>
        </convert>
        ....
      </allow>
      
    2. Add the H3SessionAjaxFilter filter to your dwr.xml, this filter provides the Hibernate session needed by the hibernate3 converter to read the un-initialized properties.
      <allow> 
        .... 
        <filter class=" org.directwebremoting.hibernate.H3SessionAjaxFilter"/>
        ....
      </allow>
      

You should use the standard bean converter if:

  1. The returned object does not have lazy properties and you want DWR to blindly serialize all properties on the object (or properties based on the exclusions, inclusions set in your DWR configuration).

Where the confusion lies:

So what was the problem that the users on the list were having?  Both users thought it was possible for DWR to make a call to the server to retrieve a lazy property when an attempt was made to access that property in JavaScript.  Here is a code sample to illustrate this point:

  function dwrCallBack(beanFromServer) {
    alert(beanFromServer.lazyProperty);
  }

DWR does a lot of magical things but it DOES NOT access the server when reading JavaScript properties.  All serialization of the object DWR will return to the browser is performed one-time – in the converter.  The converters job is to serialize the bean into a complete JavaScript object. 

In short the conversion of Hibernate beans is an all or nothing process.  The developer needs to understand what properties he needs to display in the view and prepare his converter configuration with this in mind.  Please note that the hibernate converter is very similar to the bean converter and thus you may specify include and exclude parameters (to exclude or include properties in serialization).  For more information on the bean and hibernate converters please see the DWR docs:

  1. http://directwebremoting.org/dwr/server/hibernate
  2. http://directwebremoting.org/dwr/server/dwrxml/converters/bean

If you have any questions feel free to post them in the comments area. 

3 responses to “DWR and Hibernate 3.x”

  1. Lindsey C. says:

    Thanks for the article, you confirmed my theories, though I’m still having fetching problems with exclude.

    I’ve based my app following the petclinic example in Spring 2 and my beans us a collection getter in the format:
    private Set titles;
    getTitles() <- this does retrieval with sorting and returns a List
    getTitlesInternal() <- this just gets the Set and returns a Set, getTitles() calls this function inherently

    In my DWR xml if I use exclude the titles property it never excludes it and always runs N+1 selects to fetch each, whether I configure it as a bean or hibernate3 type. My workaround is to just use include “property1,property2,property3” and then it doesn’t fetch the collections I wont need. Just wondering if you’ve seen anything like this before.

  2. Lindsey C. says:

    For anyone that stumbles accross this, ensure you exclude ALL methods that access your property.

    ie: getTitles() which references the collection members.
    AND getTitleCount() which references the collection.size().

    so the correct exclude looks like:
    dwr:exclude method=”titles, titleCount, categoryLanguages”

  3. […] (anything, including file input to java byte stream for easy uploading a file via ajax) hibernate integration, the best anti-hajacking security system, and in the upcoming 3.0 release with a fantastic […]

Leave a Reply