I’m developing a web application based on Hibernate, Spring and Wicket.
Until now I implemented the business objects and the persistence layer. The transaction is managed by a transaction interceptor of the Spring framework. So each method of the DAO classes is encapsulated in a transaction. Implementing this together with unit test was straight forward.
Now I come to the web application part where I also use Spring for dependency injection. Together with the @SpringBean annotations of the Wicket framework I inject the DAOs in the Wicket components. But as I’m pretty new to Wicket I’m a little bit stuck when using the right model for my business objects when passing them do the Wicket components.
I tried the LoadableDetachableModel but run into some issues. I got one page to created a new or edit an existing business object depending on the input parameters of the page. If there is a id in the parameters then the corresponding business object should be loaded from the database. When there are no parameters then a new business object should be created. The part were a object should be edited runs quite well but when a new object should be created, and I fill out the web form and press save, I get a NullPointerException. After some debugging I found out that LoadableDetachableModel could not return an instance of the business object as the overridden load() method could not load the object form the database as it was not saved there yet and therefore had no id.
So now I’m wondering how to solve this problem. Is the LoadableDetachableModel the right choice? Is it advisable to separate the form into two interdependent forms and each form uses a different model. So only the edit page/form uses the LoadableDetachableModel?
There’s a good explanation of this and some related issues by Igor Vaynberg at the Wicket In Action blog.
The last bit on that page deals with this issue, basically by not using a
LoadableDetachableModelbut implementing theAbstractEntityModel, which allows more complete control.