Hello,
an application I am working on uses Hibernate quite extensively for storing complex data structures. A lot of lazy fetching is used for improved performance, which fundamentally works well. Now say I have three classes: ParentClass, ChildClass (which extends ParentClass) and ContainerClass. The ContainerClass has a lazily-fetched field foo of type ParentClass (e.g. a one-to-one relation with fetch set to lazy). I store an instance of ContainerClass with foo set to an instance of ChildClass.
If now retrieve that object of type ContainerClass from the database and call getFoo(). I get a HibernateProxy as result, as I am using lazy fetching. What I do not quite understand is why this HibernateProxy extends ParentClass as oppose to ChildClass. Sure, the field is of type ParentClass but the information that the actual value is of the more specific type ChildClass is present, so why not use this?
The fact that the proxy extends the more general parent class instead of the child class becomes problematic later, e.g. when trying to determine object types using instanceof. Say ChildClasss has an equals(Object o) method that does something like return o instanceof ChildClass && [...] then the equals method will return false whenever a HibernateProxy instance exists in place of a ChildClass instance.
Why does the proxy not inherit from ChildClass but ParentClass if a ChildClass was stored? What is the most elegant way to deal with this behavior and determine the most specific class a proxy actually proxies?
Thanks
!
j4r