-->
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.  [ 4 posts ] 
Author Message
 Post subject: Need two foreign keys back to target table
PostPosted: Mon Oct 10, 2005 4:28 pm 
Beginner
Beginner

Joined: Wed Sep 21, 2005 11:52 am
Posts: 43
I'm trying to create the mappings for a Person object and a Marriage object where the Person has a
set of marriages and each marriage can refer to the bride and groom, who are Person objects.

Given the following class implementations:

Code:
public class Person {
    protected Set<Marriage> myMarriages = null;

    public Set<Marriage> getMarriages() {
        return myMarriages;
    }
    private void setMarriages(Set<Marriage> marriages) {
        myMarriages = marriages;
    }
}

public class Marriage {
    protected Person myGroom = null;
    protected Person myBride = null;
   
    public Person getBride() {
        return myBride;
    }
    public Person getGroom() {
        return myGroom;
    }
    public void setBride(Person bride) {
        myBride = bride;
    }
    public void setGroom(Person groom) {
        myGroom = groom;
    }
}

What mapping do I need to implement this model?
For marriage it seems clear that I need something like this:

Code:
<class name="org.egcrc.cfr.common.Marriage" table="MARRIAGE" lazy="true">
...
    <many-to-one
    name="myGroom"
    class="org.egcrc.cfr.common.Person"
    column="GROOM_ID"
    cascade="none"
    not-null="false"
    outer-join="false"/>

<many-to-one
    name="myBride"
    class="org.egcrc.cfr.common.Person"
    column="BRIDE_ID"
    cascade="none"
    not-null="false"
    outer-join="false"/>
</class>



But what about Person? It seems that I need a set, so I tried this:

Code:
<class name="org.egcrc.cfr.common.AbstractPerson" table="PERSON" lazy="true">
...
    <set name="myMarriages" >
      <key column="PERSON_ID"/>
      <one-to-many class="org.egcrc.cfr.common.Marriage"/>
    </set>
</class>


But this just creates a PERSON_ID column in marriage that refers to either the bride or the groom,
depending on which was set last.

I tried two sets, one using BRIDE_ID and one using GROOM_ID as the key column, but then Hibernate
throws an exception.


Hibernate version:
3.0.2

Name and version of the database you are using:
DB2
Code:


Top
 Profile  
 
 Post subject:
PostPosted: Mon Oct 10, 2005 4:40 pm 
Regular
Regular

Joined: Tue Mar 01, 2005 2:35 pm
Posts: 60
To make this work relationally, you'll need two sets in each person: brides and grooms. Now, you may be the religious type and want to enforce that only one set ever be populated, but having a join column with two Person references will put two sets in each Person entity.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Oct 10, 2005 4:55 pm 
Beginner
Beginner

Joined: Wed Sep 21, 2005 11:52 am
Posts: 43
pkulak wrote:
To make this work relationally, you'll need two sets in each person: brides and grooms. Now, you may be the religious type and want to enforce that only one set ever be populated, but having a join column with two Person references will put two sets in each Person entity.


You may be onto something but I don't completely understand your suggestion. In the domain objects, Person has a reference to a set of marriages and each marriage has a reference to the bride and the groom. Where would it make sense to have a set of brides and a set of grooms?


Top
 Profile  
 
 Post subject:
PostPosted: Mon Oct 10, 2005 7:28 pm 
Expert
Expert

Joined: Sat Jun 12, 2004 4:49 pm
Posts: 915
I don't understand complete, but I think that collections with property-ref can help
see documentation 7.6 - you have to create two property for PERSON_ID (one is non updatable, for example PERSON_BRIDE , PERSON_GROOM and make two sets for this column with property-ref)


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 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.