views:

461

answers:

2

I would like to use hibernate to contain a hierarchy of objects, however the discriminator column is a foreign key to another table that contains the CODE defining the subclass type.

Is it possible to specify the code from the joined table as the discriminator, or do I have to use the key values and hope the keys stay consistent?

e.g. discriminator="square|circle" v.s. discriminator="0|1"

table: shape

area=25   shape_type_fk=0
area=10   shape_type_fk=1

table: shape_types

ID CODE
0  square
1  circle
A: 

Looks like you need to use joined-subclass instead of subclass. Details are here.

FoxyBOA
A: 

A quick test shows that you could do this:

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
    "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"&gt;
<hibernate-mapping default-access="field">

  <class name="Shape" abstract="true">
    <id name="id"/>
    <discriminator formula="select CODE from SHAPE_TYPES st where st.ID=SHAPE_TYPE"/>
    <version name="version"/>

    <subclass name="Square" discriminator-value="square"/>
    <subclass name="Circle" discriminator-value="circle"/>

  </class>
</hibernate-mapping>

Now this mapping will not create the SHARE_TYPE table that is referenced in the formula Nor does it add the SHAPE_TYPE column to the SHAPE table. This you could do in the import.sql file that Hibernate reads automatically from the classpath or by mapping another class to this table.

Maarten Winkels
Maarten's examples works, but needs parens around the discriminator formula, and columns in the subselect must reference the aliased table. Also, using criteria for a subclass results in a correlated subquery, which is very inefficient. I guess I'll have to live without the polymorphic (table per) hierachy.
gbegley