Hi all,
I have run into an issue with a 3-level class hierarchy and I wonder if there is some nice way to solve it.
I have a class hierarchy with a parent, 2 childs and a grand-child, mapped like this.
Code:
<class name="Resource" table="RESOURCE" entity-name="RESOURCE">
<discriminator column="TYPE" type="string" length="1" />
<subclass class="Group" discriminator-value="G" entity-name="GROUP">
</subclass>
<subclass class="User" entity-name="USER">
<subclass class="RegisteredUser" discriminator-value="R" entity-name="REGISTERED_USER">
</subclass>
<subclass class="UnregisteredUser" discriminator-value="U" entity-name="UNREGISTERED_USER">
</subclass>
</subclass>
</class>
and some other class with an assoziation
Code:
<many-to-one name="owner" column="OWNER_ID" entity-name="RESOURCE" />
Now, checking the owner class along
Code:
obj.getOwner().getClass().getName()
I get the real class for a Group (which is on the 1st level of the hierarchy) and instanceof etc. work nicely. Howver, for an UnregisteredUser as well as a RegisteredUser (2nd level of the hierarchy) I usually get a Proxy and all kind of comparisons to the expected classes as well as casting fails.
I found that the issue does vanish if I turn off lazy-fetching on the assocization and explicitly loading the owner by ID anew will result in the correct class as well. I tried using abstract="true" on the common parent ("User") as it is actually an abstract class and tried setting a discriminator for it as well - neither helps.
Is there any other "nicer" way than turning of lazy-fetching or reloading the object to be able to cast or check the class?
Cheers,
Sven