Models Inc. / HibernateObjectModel
This is the third entry in a series that is to become Databinder 101, an introductory text for the toolkit.
Happily, IModel’s detach logic provides an answer to the problem of carrying Hibernate entities between web requests. If we discard our references to the entities before the session ends, and reload them before use in a new session, it’s almost like we had the same object all along.
Almost. Any changes that weren’t persisted to the database will be undone. The programmer must either commit changes (as done by default in DataForm.onSubmit()) or let them evaporate. This isn’t so bad really, compared to practically every Web application design, but it is a little grating. Future versions of Databinder will attempt Hibernate sessions that span requests, but until then we have to think in terms of committing changes at every request.
With that caveat behind us, we can use HibernateObjectModel as our good ship entity-pop. As long as any entity loaded by Hibernate goes into one of these, we can use it without fear for the next one thousand requests. It can travel between components, and even between pages.
It is the session babysitter.
To be continued…