-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 
Author Message
 Post subject: Mixture of Inheritance techniques
PostPosted: Mon May 10, 2010 1:03 am 
Newbie

Joined: Fri Apr 18, 2008 1:07 am
Posts: 14
Location: Pakistan
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.xml
Code:
<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.xml
Code:
<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.xml
Code:
<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.xml
Code:
<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.xml
Code:
<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


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.