Hibernate version: 3.0.5
I have a baffling (to me at least :)) situation: Consider the following
class hierarcy:
Code:
class A { int id; }
class B extends A { String name; }
class SomeClass { A refA; }
class SomeOtherClass { A refB; }
and the database has the following tables and data:
Code:
TABLE A: ID REF_TYPE NAME
1100 B SomeName
TABLE SOME_CLASS: ID A_ ID
1000 1100
TABLE SOME_OTHER_CLASS: ID B_ID
1200 1100
and the following instance set after some queries:
Code:
ObjectOfA : A$$EnhancedByCGLIB$$234332@1100
ObjectOfSomeClass : SomeClass@1000
refA = ObjectOfA
For the purpose of this discussion, ObjectOfSomeClass is an instance of SomeClass which was loaded as a result of a load/query and its refA
member points to ObjectOfA, an instance of *B* was loaded
and was initialised as a proxy so the real class is the CGLIB enhanced
class.
Now, when an instance of SomeOtherClass is required, and when the
instance has its refB member pointing to the same instance of B that is
in the session cache, the same reference value (1100) is being set
in the instance - that is the enhanced class version.
This is not a problem except when the following takes place:
Code:
B someB = (B)ObjectOfSomeClass.getRefB();
The cast fails since the actual type is the enhanced class.
To compound the confusion, sometimes the reference is the
actual type - B.
What gives? I would have thought that since the correct mapping is
provided, whenever a reference to B in an instance of SomeOtherClass
is made, interception will ensure that the actual target instance is
returned.
What am I missing? Any help will be greatly appreciated