-->
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: strange situation
PostPosted: Mon Mar 21, 2005 1:50 pm 
Newbie

Joined: Fri Mar 18, 2005 3:47 pm
Posts: 1
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.


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.