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.. :)