28 April 2010

Dealing with Hibernate proxies

To deal with lazy loading Hibernate uses proxy objects.
Pitfalls for this approach have been documented. Basically the instanceof operator can fail on a sublcass of an entity.
The bug ticket on this has been rejected, with typical JBoss flair:

If you think this through a bit more carefully, you will see why it is impossible.
Gavin King
Still JPA compliancy requires the instanceof operator to work correctly.
Here are some ways to deal with the proxies: