DWR and Hibernate
Check-list to get DWR working with Hibernate
- Make sure you are on the latest DWR. The Hibernate converter is fairly new so you will need a recent download.
- Make sure you are happy with everything on the getting started page.
- Make sure your Hibernate configuration works properly outside of DWR.
- If you are using Spring with Hibernate then you might want to check out the Spring integration first.
- Configure DWR to work with Hibernate. (See below).
- Look at the demo pages:
http://localhost:8080/[YOUR-WEBAPP]/dwrto check that your spring beans appear.
The HibernateBeanConverter
This converter is very similar to the standard BeanConverter except that we can decide what to do with lazy loaded properties.
In DWR 1.1 the Hibernate converter is called "hibernate" and it works for Hibernate 2 and 3. In DWR 2.x, there are 2 converters called "hibernate2" and (predictably) "hibernate3".
Using HibernateBeanConverter may be risky for a number of reasons:
- Architecture: HibernateBeanConverter does not follow an MVC pattern which would keep objects from a data layer separate from the view layer. This risk can also be mitigated using a separate set of beans as above.
- Performance: DWR attempts to marshall all reachable properties in a similar way to Serialization (except that DWR reads JavaBean properties only). So it is easy to create a mapping that ends up serializing your entire database over HTTP. Usually this is not what you want. This risk can be mitigated using the exclusion feature of BeanConverter (from which HibernateBeanConverter is derived):
<param name="exclude" value="propertyToExclude1, propertyToExclude2"/>
The HibernateBeanConverter tries to avoid reading from un-initialized properties. (If you just want something that blindly reads everything then just use a plain BeanConverter).
Hibernate 3 is considerably better at helping in this respect, and indeed under Hibernate 2 you may well discover that you are getting many empty beans as a result.
Session Management
If you are using Hibernate object the you need to understand that each DWR request is a new servlet request so you need to ensure that a Hibernate Session is open for you to work with.
If you are using Spring then an easy option is to use the Spring OpenSessionInViewFilter which will ensure that a Hibernate Session is open. Similar solutions are available in many frameworks.