-->
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 - changing subclass
PostPosted: Tue Jun 07, 2005 9:02 am 
Beginner
Beginner

Joined: Tue Apr 05, 2005 12:09 pm
Posts: 48
Location: Slovakia (SK), Košice (KE)
Hibernate version: 3

Maybe I want too much from hibernate, but is it possible to change a type of the subclass?

I vave 3 tables: CAT, DOMESTIC_CAT, WILD_CAT. DOMESTIC_CAT, WILD_CAT is in one-to-one relation with CAT. These tables are mapped to a 3 classes - Cat (abstract) and subclasses - DomesticCat and WildCat.

Code:
<class name="Cat" table="CAT">
   ... some properties ...
   <joined-subclass name="DomesticCat" table="DOMESTIC_CAT">
         <key column="CAT_ID" />
         ... some properties ...
   </joined-subclass>
   <joined-subclass name="WildCat" table="WILD_CAT">
         <key column="CAT_ID" />
         ... some properties ...
   </joined-subclass>
</class>


Code:
Cat cat = session.get(WildCat.class, id);
// now somehow change the type to DomesticCat
DomesticCat cat2 = changeTypeToDomesticCat(cat);
cat2.setOwner("Fred");
session.update(cat2); // ???


Is this possible in Hibernate? If yes, then how? Using JDBC I would delete one row from WILD_CAT and add one row to DOMESTIC_CAT. Using hibernate I can now only delete the "old" cat completly (delete two records - from table CAT and WILD_CAT) and then add new cat (insert two records - to table CAT and DOMESTIC_CAT). Moreover if cat uses some other child relations, they must be deleted / inserted too (without any change to their properties).

Thanks in advance
Martin


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jun 07, 2005 9:40 am 
Beginner
Beginner

Joined: Tue Apr 05, 2005 12:09 pm
Posts: 48
Location: Slovakia (SK), Košice (KE)
I know that of course :-). But using JDBC I could solve my problem that I can't solve using Hibernate yet. Am I use wrong mapping? I think my problem is not too exotic. How would you solve this issue (using Hibernate)? Should I instead try to use "Table per concrete class" and create tables CAT, CAT_TYPE_DOMESTIC and CAT_TYPE_WILD and:

Code:
class Cat {
   private CatType catType;
   // getter & setter for catType

   // ... other props
}


Using this design the change of cat type wold mean deleting old catType and adding a new one. I've tried such a design before, but I've encountered another problem - see http://forum.hibernate.org/viewtopic.php?t=943387. I don't have any more time to again redesign my application without being sure that it will work.

What do you think? Please help!

Thanks in advance
--
Martin


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jun 07, 2005 10:55 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
When you tell me how an object can change classes in Java, I will show you how to implement this in ORM.


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.