joined-subclass with discriminator-value?
July 22, 2006 – 18:05 | hibernate, javaSo I am trying to use Hibernate to map a class hierarchy with table-per-hierarchy mixed with table-per-subclass.
I start with:
-
<class name="com.packageA.SuperClass" discriminator-value="0" table="SUPER">
-
<discriminator column="CLASS_TYPE" type="int" />
-
<!-- ... -->
-
<subclass name="com.packageA.SubClass1" discriminator-value="1">
-
<!-- ... -->
-
</subclass>
-
</class>
and to add a second SubClass2 from another package packageB, which I wish to have a join table for the extended properties. Now in the overall design, it's OK for packageB to depend on packageA, but not the other way around, so I want to keep SubClass2's Hibernate mapping in packageB as well. Therefore, initially I tried, in SubClass2's own mapping file:
-
<joined -subclass name="com.packageB.SubClass2" extends="com.packageA.SuperClass" table="SUB2">
-
<!-- ... -->
-
</joined>
(Note: apparently there is some bug in Wordpress that mangles up the opening and closing tags for "joined-subclass". It's properly opened and closed in my actual XML code.)
But Hibernate threw back a NumberFormatException when trying to convert a string discriminator value, "com.packageB.SubClass2", to an integer, apparently due to the fact that the joined-subclass above didn't give a discriminator value in integer, so it defaults to the class FQN. And of course, the <joined-subclass> element can't have a 'discriminator-value' attribute per the DTD.
Now I could probably just get rid of my integer discriminator scheme and use the default class FQNs. But I never thought class FQNs are good candidates for class type discriminator to begin with, because... well, why would I want to have to screw around in the DB running massive SQL updates just because some class is renamed?
So I end up sacrificing a little on the dependency rule front, and basically moved SubClass2's mapping into SuperClass's:
-
<class name="com.packageA.SuperClass" discriminator-value="0" table="SUPER">
-
<discriminator column="CLASS_TYPE" type="int" />
-
<!-- ... -->
-
<subclass name="com.packageA.SubClass1" discriminator-value="1">
-
<!-- ... -->
-
</subclass>
-
<subclass name="com.packageB.SubClass2" discriminator-value="2">
-
<join table="SUB2">
-
<!-- ... -->
-
</join>
-
</subclass>
-
</class>
I still can't see why joined-subclass shouldn't have that 'discriminator-value' attribute, though.
