There is some discussion going on in my place of employment about Hibernate's behavior in always returning new instances of objects from the second-level cache rather than returning a single cached instance (in comparsion with TopLink, which does the latter). The issue under discussion is the efficiency of creating unnecessary instances. I personally prefer the way Hibernate does it because it just seems generally safer, but I'm wondering if there is an option for obtaining a sort of "readonly" instance which could be shared between multiple users/threads safely because it will never be modified. The developer would take responsibility for ensuring that no changes will be made to the object's state. In other words, you might have a "findForEdit" and a "findForView" method for an entity. The former will always give you your own instance, and the latter would give you a shared instance that you have to make sure is never modified.
|