How to add an extra constraint to joined-subclass in hibernate

240 Views Asked by At

I am programming a Web-Application with Java EE and JSF. As a data link layer, I am using hibernate.

Master and Slave classes extends Device class. They uses primary key of Device table - "DEVICE_ID". I want to know which Slave record is related with a Master record. Because of this, I need to add extra constraint into Slave table.

How can I do that? Please help me! Any feedback is appreciated!

Device.hbm.xml;

<hibernate-mapping>
<class name="deviceManagement.Device" table="DEVICE">
    <id name="DEVICE_ID" type="int">
        <column name="DEVICE_ID" />
        <generator class="increment" />
    </id>
    <property name="DEVICE_NAME" type="java.lang.String">
        <column name="DEVICE_NAME" unique="true" />
    </property>
    <property name="DEVICE_TYPE" type="java.lang.String">
        <column name="DEVICE_TYPE" />
    </property>
...

Slave.hbm.xml;

<hibernate-mapping>
<joined-subclass name="deviceManagement.Slave" extends="deviceManagement.Device" table="SLAVE" lazy="false">
    <key>
        <column name="DEVICE_ID" />
    </key>
    <property name="LUMINAIRE_MODEL" type="java.lang.String">
        <column name="LUMINAIRE_MODEL" />
    </property>
...

Master.hbm.xml;

<hibernate-mapping>
<joined-subclass name="deviceManagement.Master" extends="deviceManagement.Device" table="MASTER" lazy="false">
    <key>
        <column name="DEVICE_ID" />
    </key>
    <property name="HOST_NAME" type="java.lang.String">
        <column name="HOST_NAME" />
    </property>
...
1

There are 1 best solutions below

1
On

you can add a column to slave.hbm.xml that will be a foreign key to a Master entity, you have to define the fetch type , so hibernate will handle the join while you fetching a slave entity and will have the master entity initialized