-->
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: mapping collection of joined-subclass by parent field
PostPosted: Wed Jul 13, 2005 6:43 pm 
Regular
Regular

Joined: Thu May 26, 2005 12:20 am
Posts: 72
Hibernate version: 3.0.4

Name and version of the database you are using: Oracle 10g

Not sure how/why this works the way it works, but I think there has to be a simple solution within hibernate.

There are 3 objects involved:
- User
- Asset
- VideoAsset (joined-subclass of Asset)

All Asset objects are owned by a User.
Code:
<class   name="Asset"   table="Asset">
   <id   column="id"   name="id"   type="long"   unsaved-value="0">
      <generator   class="native"   />
   </id>

   <many-to-one name="user" column="user_id" class="User" />

   <joined-subclass   name="VideoAsset"   table="Video_Asset">
      <key   column="id"   />
   </joined-subclass>
</class>


I would like to have sets of the concrete subclasses into User.

Code:
<class   name="User"   table="MyUser">
   <id   column="id"   name="id"   type="long"   unsaved-value="0">
      <generator   class="native"   />
   </id>

   <set name="videoAssets">
      <key column="user_id" />
      <one-to-many class="VideoAsset"  />
   </set>
</class>


But the above doesnt work because it looks for user_id in the Video_Asset table. In fact, when I use the above mapping with hibernate DB auto-gen, it creates a column user_id in the table Video_Asset. Then User.getVideoAssets() only returns those assets that have the Video_Assets.user_id set. (And saving a Video_Asset sets the user_id in the Asset table.

Something like:

Code:
<class   name="User"   table="MyUser">
   <id   column="id"   name="id"   type="long"   unsaved-value="0">
      <generator   class="native"   />
   </id>

   <set name="videoAssets">
      <key column="user_id" table="Asset" />
      <one-to-many class="VideoAsset"  />
   </set>
</class>


But really, I would think that it would automatically look in the parent class for the key. *sigh*

Thoughts? Thank you.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jul 20, 2005 3:09 am 
Newbie

Joined: Thu Jan 13, 2005 6:24 pm
Posts: 12
I found several posts on this problem and haven't seen any answers. I am also facing the same exact issue where the <key/> in a collection always maps to the derived class table even when the foreign key is actually in the base class. It seems to me that the easy fix would be to allow specifying the table name to use for the foreign key. A nicer way would be to automatically determine which table the key is in through reflection.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jul 21, 2005 6:44 pm 
Regular
Regular

Joined: Thu May 26, 2005 12:20 am
Posts: 72
I have eventually gotten around the problem by creating view tables for my subclasses that mimic the "table per concrete class" approach (or at least whichever additional colmns I need to access) and then mapping my hibernate file to the View for the subclass, with the duplicated parent column rows either not mapped in the mapping file (for collection sorts it's not necessary) or listed with insert/update=false.

I still actually have the underlying tables set up as a "table per subclass" approach which is how I want my data organized in the event that I remove the hibernate layer.

If anyone wants me to elaborate I can, but I am just trying it now so maybe I will run into more issues down the road. =)


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.