Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hibernate how to discriminate in joined-subclass

Tags:

hibernate

Supposing I have the following case of Entity model and using Hibernate 3.6:

Person
    |__ Student
            |__ SchoolBoy
            |__ CollegeStudent

The tables I have in my DB are tperson and tstudent. As I'm using hibernate mapping files, I declare it as below into my Person entity definition:

<joined-subclass name="Student" table="tstudent">
    <key column="id_person" />
</joined-subclass>

Being SchoolBoy similar to CollegeStudent, I want to use the same table for both (tstudent), having a student-type discriminator column, while I keep both of the classes. My problem is that Hibernate doesn't seem to allow discriminator columns once you're into a joined-subclass.

Is there a workaround for that?

UPDATE

That's what I have right now:

<class name="Person">
    <id name="Id" column="id" type="integer">
        <generator class="increment" />
    </id>
    <joined-subclass name="Student" table="tstudent">
        <key column="id_person" />
        <joined-subclass name="SchoolBoy" table="tschoolboy">
            <key column="id_person" />
        </joined-subclass>
        <joined-subclass name="CollegeStudent" table="tcollegestudent">
            <key column="id_person" />
        </joined-subclass>
    </joined-subclass>
</class>

That's what I would like to have in the join part of the mapping:

<joined-subclass name="Student" table="tstudent">
    <key column="id_person" />
    <discriminator column="student_type" />
    <subclass name="SchoolBoy" discriminator-value="SCHOOL_BOY" />
    <subclass name="CollegeStudent" discriminator-value="COLLEGE_STUDENT" />
</joined-subclass>
like image 917
Xtreme Biker Avatar asked Jul 04 '13 09:07

Xtreme Biker


1 Answers

The Hibernate documentation says:

Hibernate does not support mixing <subclass>, <joined-subclass> and <union-subclass> mappings under the same root <class> element. It is possible to mix together the table per hierarchy and table per subclass strategies under the the same <class> element, by combining the <subclass> and <join> elements

In your case, you would be required to move the discriminator column to the tperson table and use join elements to add extra properties to you student classes.

<class name="Person" table="tperson" discriminator-value="PERSON">
    <id name="Id" column="id" type="integer">
        <generator class="increment" />
    </id>
    <discriminator column="person_type" />
    <subclass name="Student" discriminator-value="STUDENT">
        <key column="id_person" />
        <subclass name="SchoolBoy" discriminator-value="SCHOOL_BOY">
            <join table="tstudent">
                <key column="id_person" />
                ...
            </join>
        </subclass>
        <subclass name="CollegeStudent" discriminator-value="COLLEGE_STUDENT">
            <join table="tstudent">
                <key column="id_person" />
                ...
            </join>
        </subclass>
    </subclass>
</class>

You can also probably add a join in the Student subclass element for the properties that are common to all students

like image 188
Guillaume Avatar answered Sep 21 '22 20:09

Guillaume