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:
- A converter (hibernate3) that attempts to avoid reading from un-initialized (lazy) properties.
- 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:
- 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.
- Add a converter to your dwr.xml:
<allow> .... <convert converter="hibernate3" match="yourpackage.yourClass" /> .... </allow>
- Add a converter to your dwr.xml:
- The returned object has lazy properties and you want DWR to attempt to serialize these properties.
- 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>
- 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>
- 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:
You should use the standard bean converter if:
- 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:
- http://directwebremoting.org/dwr/server/hibernate
- http://directwebremoting.org/dwr/server/dwrxml/converters/bean
If you have any questions feel free to post them in the comments area.