Hi mates,
I am facing a scenario where the mixture of the two inheritance techniques;
Table per class and
Table per hierarchy is needed.
I have 3 tables.
------------------
mobile_device
------------------
device_id
person_id (FK)
mobile_info
-----------------
-----------------------
computer_device
-----------------------
device_id
person_id (FK)
processor
laptop_desktop_info
------------------------
---------------------
person
---------------------
person_id
device_type (FK)
----------------------
In the above scenario, the
computer_device_info table is the table per hierarchy table and the discriminator is the device_type field in the person table that can be joined through the person_id .
Here are my mappings.
base_device.hbm.xmlCode:
<hibernate-mapping package="com.demo.beans">
<class name="BaseDevice">
<id name="deviceId" column="DEVICE_ID" type="int">
<generator class="increment"></generator>
</id>
<discriminator
formula="(select P.DEVICE_TYPE from PERSON P where P.PERSON_ID=PERSON_ID)"></discriminator>
<many-to-one name="person" class="Person">
<column name="PERSON_ID"></column>
</many-to-one>
</class>
</hibernate-mapping>
computer_device.hbm.xmlCode:
<hibernate-mapping package="com.demo.beans">
<union-subclass name="ComputerDevice" table="COMPUTER_DEVICE" extends="BaseDevice">
<property name="processor" column="PROCESSOR" type="string"></property>
</union-subclass>
</hibernate-mapping>
tv_device.hbm.xmlCode:
<hibernate-mapping package="com.demo.beans">
<union-subclass name="MobileDevice" table="MOBILE_DEVICE" extends="BaseDevice">
<property name="mobile_info" column="MOBILE_INFO" type="string"></property>
</union-subclass>
</hibernate-mapping>
laptop_device.hbm.xmlCode:
<hibernate-mapping package="com.demo.beans">
<subclass name="LaptopDevice" discriminator-value="LPT"
extends="ComputerDevice">
<property name="laptopInfo" column="LAPTOP_DESKTOP_INFO"></property>
</subclass>
</hibernate-mapping>
desktop_device.hbm.xmlCode:
<hibernate-mapping package="com.demo.beans">
<subclass name="DesktopDevice" discriminator-value="DST"
extends="ComputerDevice">
<property name="desktopInfo" column="LAPTOP_DESKTOP_INFO"></property>
</subclass>
</hibernate-mapping>
person.hbm.xml<hibernate-mapping package="com.demo.beans">
<class name="Person" table="PERSON">
<id name="personId" column="PERSON_ID" type="int">
<generator class="increment"></generator>
</id>
<property name="deviceType" column="DEVICE_TYPE" type="string"></property>
<set name="devices">
<key column="PERSON_ID"></key>
<one-to-many class="BaseDevice" />
</set>
</class>
</hibernate-mapping>
So, basically, I have two levels of hierarchy.
BaseDevice
_______|________
|.........................|
MobileDevice ComputerDevice
________|_________
|............................|
DesktopDevice LaptopDevice
The first level is table per concrete class technique while the second level is the table per class hierarchy technique. Everything compiles and initializes well. But the problem arises I get an object of Person by calling personDAO.get(1).
When I check the type of each device in the collection person.getDevices(), it is always Laptop. Even though there are records with the DST discriminator value.
What may I be doing wrong in this scenario? Please help me.
Regards,
Kashif