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.  [ 3 posts ] 
Author Message
 Post subject: table-per-subclass; 2 subclasses, an entity is both
PostPosted: Mon Jan 31, 2005 7:43 pm 
Newbie

Joined: Thu Dec 30, 2004 6:58 pm
Posts: 6
We've run into a problem with the table-per-subclass strategy. We have the following simple hierarchy:
External_Organization
Vendor (extends External Organization)
Manufacturer (extends External Organization)

Some External Organizations might be a Vendor AND a Manufacturer. Hibernate doesn't seem to like this.

We have created a table for each of these entities. Each table has as its key a column named ORG_ID.

How can we get this to work?

Here is our mapping:
Code:
<hibernate-mapping>

<class name="ExternalOrganization"
      table="EXTERNAL_ORG_TBL"
      lazy="false">
       
    <cache usage="nonstrict-read-write"/>

   <!-- Common id property. -->
   <id name="orgId"
      type="long"
      column="ORG_ID"
      unsaved-value="null">
      <generator class="sequence">
            <param name="sequence">ENTERPRISE_SEQ</param>
        </generator>
   </id>
   
    <version name="version" column="VERSION" />
   
    <!-- properties removed for brevity -->
               
    <!-- Vendor subclass mapping to its own table, normalized. -->
   <joined-subclass name="Vendor"
                table="VENDOR_TBL"
                lazy="false">

   <key  column="ORG_ID" />
       
        <!-- properties removed for brevity -->   
     
     </joined-subclass>
   
    <!-- Manufacturer subclass mapping to its own table, normalized. -->
   <joined-subclass name="Manufacturer"
                table="MANUFACTURER_TBL"
                lazy="false">
                     
        <key  column="ORG_ID" />
   
        <!-- properties removed for brevity-->
                     
     </joined-subclass>

</class>

</hibernate-mapping>


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 01, 2005 12:01 pm 
Newbie

Joined: Thu Dec 30, 2004 6:58 pm
Posts: 6
just to add - I'm just hoping someone can comment on whether or not this WOULD be a problem. Shouldn't Hibernate be able to handle this situation?
Thanks...


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 01, 2005 1:40 pm 
Newbie

Joined: Thu Dec 30, 2004 6:58 pm
Posts: 6
I wrote a simple test case to illustrate what's happening. It's clearer to me know what's happening. Say Dell Computers, as an External Organization, is a Vendor AND a Manufacturer. In our DB, Dell Computers has the ORG_ID 47599.
When I run the following test case, I get the error shown below. I see that Hibernate naturally does not like to have 2 ExternalOrganization instances with the same Id.

Code:
public void testSetVendorAndManufacturer() throws Exception {
        Vendor vendor = vendorDAO.getVendorById(new Long(47599), false);
        Manufacturer manufacturer =    manufacturerDAO.getManufacturerById(new Long(47599), false);
       
        Item item = itemDAO.getItemById(new Long(47852), false);
        item.addVendor(vendor);
        item.setManufacturer(manufacturer);
    }


Here is the error:
Code:
There was 1 error:

1) testSetVendorAndManufacturer(us.mn.state.health.test.TestItem)java.lang.ClassCastException: us.mn.state.health.model.common.Vendor
at us.mn.state.health.dao.HibernateManufacturerDAO.getManufacturerById(HibernateManufacturerDAO.java:32)
at us.mn.state.health.test.TestItem.testSetVendorAndManufacturer(TestItem.java:205)
at
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)



If I change the order in which I try to load the Vendor and Manufacturer object (ie, try to load the Manufacturer first), I get the same ClassCastException except its on a Manufacturer instead (ie, trying to cast a Vendor into a Manufacturer).

Certainly others have run into this problem! Right? What are we doing wrong? Do we have to use composition in our subclasses instead of Aggregration/Inheritance?
We're hoping there's some Hibernate tricks we don't know about.. :)


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

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.