We are using Wicket with Hibernate in the background.
As part of out UI we have quite long running conversations spanning multiple requests before the updated information is written back to the database.
To avoid getting hibernate errors with detached objects we are now using value objects to transfer info from the service layer to Wicket.
However we now end up with an explosion of almost the same objects :
e.g.
This plumbing becomes exponentially worse when collections to other objects are involved in the objects.
There has to be a better way to organize this.
Can anyone share tips to make this less onerous?
Maybe make the value objects mutable so we can remove the need for a seaprate backing bean in Wicket?
Use the entity beans but absolutely make dead-certain they are detached from hibernate. (easier said than done)?
Some other tricks or patterns?
Use OpenSessionInViewFilter from Spring. This will help you in creating a hibernate session per-request. However, it only opens a read-only session. To perform any write operation to your entity beans, I would recommend to start a hibernate transaction (you can use TransactionTemplate from Spring for this). There are other methods to perform a write operation, but I found this was easiest for me.
Now, to clean up your beans, here's what I tend to do
I would go with (2) above as it is pretty simple and would work on most cases. Its not recommended when your persistent beans are modified by two separate sessions as you'll end up overriding the previous updates.
Hibernate do have some documentation on dealing with detached objects.
the usual solution would be the open session in view "pattern", see e.g. osiv with wicket
i have no good experiences with OSIV, so i rather advise to set transaction boundaries below the GUI layer and solve the infamous lazyInitializationException with clever planned data retrieval in business or service layer
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With