-->
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: Maping one-to-many collections on a non-primary key
PostPosted: Wed Sep 27, 2006 1:11 am 
Beginner
Beginner

Joined: Tue Sep 26, 2006 11:46 pm
Posts: 33
I'm attempting to create a one-to-many collection that is joined on an arbitrary column of the parent table rather than on the primary key. That is, I want the following tables:

Parent
Code:
Parent
------
uniqueId (PK)
otherId
.....


Child
-----
uniqueId (PK)
parentOtherId
.......


Where rather than parentOtherId being a foreign key to uniqueId of the Parent table, it's a foreign key to the otherId column of Parent. That is, when loading the children of parent the SQL executed is something along the lines of
Code:
SELECT * FROM Child where parentOtherId = ?


I've been unable to find a way of mapping this relationship so that hibernate will load the collection appropriately. Rather than load

The reason I want this odd mapping is I wish to maintain a version history of all changes to the mapped objects. Unfortunately the history table solution suggested in a number of forum posts won't work as I need to be able to load previous versions of the entire object graph as efficiently as the current version. So I want all versions including the current in a single table with the associations based off a separate key from the database primary key. Then I can use filters to restrict the loaded objects for a given session to the current version or the version as of some date.

Thanks in advance.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Sep 27, 2006 4:04 am 
Beginner
Beginner

Joined: Fri Jun 02, 2006 1:23 am
Posts: 27
There is a property-ref attribute you can use. According to the docs,

Quote:
property-ref: (optional) The name of a property of the associated class that is joined to this foreign key. If not specified, the primary key of the associated class is used.

So you would do something like,

Code:
<hibernate-mapping>
   <class name="Parent" table="parent">
      <id name="uniqueId" />
      <property name="otherId" />
       
        <set name="children">
            <key column="parentOtherId" property-ref="otherId" />
            <one-to-many class="Child"/>
        </set>       
       
   </class>

   <class name="Child" table="child">
      <id name="uniqueId" />
   </class>
</hibernate-mapping>


I had a similar problem. Unfortunately I haven't gotten any help :(

http://forum.hibernate.org/viewtopic.php?t=964964

_________________
George F.
Don't forget to rate!


Top
 Profile  
 
 Post subject:
PostPosted: Wed Sep 27, 2006 11:12 pm 
Beginner
Beginner

Joined: Tue Sep 26, 2006 11:46 pm
Posts: 33
Cheers for that, it's almost exactly what I needed. Unfortunately as I can't use bi-directional associations with it as Hibernate complains that there isn't a unique row for the parent reference (justifiably so). And uni-directional associations result in a spurious update to set the parent reference which isn't going to fly for us. Has anyone managed to get around this?

Also there doesn't appear to be an equivalent of the property-ref in Annotations, is anyone aware of a way to do this without having to use the XML mapping file?


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.