Hi,
assume you have the following mapping:
[code]
<hibernate-mapping default-access="field">
<class name="AbstractClass" abstract="true">
<id name="id">
<generator class="increment"/>
</id>
<union-subclass name="Concrete1" table="c1">
<property name="p1"/>
</union-subclass>
<union-subclass name="Concrete2" table="c2">
<property name="p2"/>
</union-subclass>
</class>
<class name="Test">
<id name="id">
<generator class="increment"/>
</id>
<many-to-one name="x" class="AbstractClass" column="ref"/>
</class>
</hibernate-mapping>
[/code]
When I fetch all objects of type "AbstractClass", i can test if they are of type "Concrete1" or "Concrete2" and then cast them.
When I fetch all objects of type "Test" and then access the attribute "x", the result is a CGLib Proxy which extends "AbstractClass". This is afaik a known problem which results from the fact that the type of the object is known only after it's queried.
Now my question is how to workaround this.
1. I could set "lazy=false" for the mapping of "AbstractClass". This worked out-of-the-box for me. But what if the abstract class (or onde of it's implementations) contains a maped collection? will this cause that these collections are always fetched or will this only cause that neither for AbstractClass nor for the Implementations a proxy will be creared?
2. Maybe i could disable lazy-loading of the many-to-one in "Test". I think this should cause that the related class (Concrete1 or Concrete2) will loaded together with Test.
The intereting thing is that when i set lazy=false for AbstractClass and then load "Test", the related Concreate class is loaded togehter with "Test". When i instead set lazy=false for the many-to-one, it has no effect at all.
So the first way (disabling lazy fetching of AbstractClass) is currently the only thing i found to make this work. Any idea how to do it better?
Thank you,
Michael.
|