Read the rules before posting!
http://www.hibernate.org/ForumMailingli ... AskForHelp
Hibernate version: 2.1.7
Hello,
here is my situation:
Table B and C extend Table A. B and C are in a <joined subclass /> of A in A.hbm, where B's and C's primary key ref's A:
Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 2.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd" >
<hibernate-mapping>
<class name="ClassA" table="table_a">
<id name="serialNumber" type="java.lang.String" column="a_serial_number">
<generator class="assigned" />
</id>
<property name="type" type="int" column="type" length="11" />
<joined-subclass name="ClassB" table="table_b" extends="ClassA">
<key column="b_serial_number" />
<property name="name" type="java.lang.String" column="name" length="64" />
</joined-subclass>
<joined-subclass name="ClassC" table="table_c" extends="ClassA">
<key column="c_serial_number" />
<property name="name" type="java.lang.String" column="name" length="64" />
</joined-subclass>
</class>
</hibernate-mapping>
when i run this in my app:
Code:
...
B b = new B();
b.setSerialNumber("SN1");
session.save(b);
...
table B and A get populated with the serial number (no problem here).
BUT, there will be times where do not know if the object i am persisting is either class B or class C, so i would instantiate class A instead:
Code:
...
A a = new A();
a.setSerialNumber("SN2");
session.save(a);
...
Eventually,in another session, my app finds out which subclass (for this ex, let's say it is of class B) it is and wants to update in the datastore. my app will have the SN and attempt to insert into table by calling save() function on object b:
Code:
...
B b = new B();
b.setSerialNumber("SN2");
session.save(b);
...
This will obviously NOT work b/c it will attempt to insert in table A with a duplicate key.
is there some trick to handle this situation? any suggestions would be greatly appreciated.
thanks much.