The problem of using Hibernate - or any ORM tool using lazy initialization for that matter - in a multi-tier architecture which physically separates the persistence tier from the presentation tier seems to be an inexhaustible well of debate.
In Hibernate's Wiki, Steve Ebersole describes a solution using DTOs specifically tuned to the needs of the application at hand. As a potential drawback he mentions the extra work needed to code these DTOs. Moreover, one might argue that DTOs in general exhibit Martin Fowler's code smell of parallel class hierarchies as described in Hibernate in Action, though this might be a matter of personal taste. Mind you, I don't say that this solution is bad. Trying to balance simplicity and performance is always a hard task which - in my view - demands a compromise to be made.
The Wiki also links to a post in which the author described a solution based on CGLIB proxies which initialize collection properties on demand, caching the results. At first glance, I find this solution attractive yet a quick survey on the internet revealed that it seems to be rarely used, if at all. Moreover, its imlementation seems to be problematic, since people who embarked on this road often struggle with ObjectNotUniqueExceptions, if I recall correctly.
So before I go about implementing this approach myself - I have never worked with dynamic proxies - I would like to know if there is anybody out there who has taken this road and would be willing to share his experiences. Is it a viable solution? If yes, what are the problems to look out for? Of course, code samples would be great ;-) Just dreaming ...
Many thanks for any pointers,
Olaf Bergner
|