joined-subclass with discriminator-value?

July 22, 2006 – 18:05 | hibernate, java

So I am trying to use Hibernate to map a class hierarchy with table-per-hierarchy mixed with table-per-subclass.

I start with:

XML:
  1. <class name="com.packageA.SuperClass" discriminator-value="0" table="SUPER">
  2.   <discriminator column="CLASS_TYPE" type="int" />
  3.   <!-- ... -->
  4.   <subclass name="com.packageA.SubClass1" discriminator-value="1">
  5.     <!-- ... -->
  6.   </subclass>
  7. </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:

XML:
  1. <joined -subclass name="com.packageB.SubClass2" extends="com.packageA.SuperClass"  table="SUB2">
  2.   <!-- ... -->
  3. </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:

XML:
  1. <class name="com.packageA.SuperClass" discriminator-value="0" table="SUPER">
  2.   <discriminator column="CLASS_TYPE" type="int" />
  3.   <!-- ... -->
  4.   <subclass name="com.packageA.SubClass1" discriminator-value="1">
  5.     <!-- ... -->
  6.   </subclass>
  7.   <subclass name="com.packageB.SubClass2" discriminator-value="2">
  8.     <join table="SUB2">
  9.       <!-- ... -->
  10.     </join>
  11.   </subclass>
  12. </class>

I still can't see why joined-subclass shouldn't have that 'discriminator-value' attribute, though.

Trackback from your site, or follow the comments in RSS.

Post a Comment