-->
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: Don't know how to map an strange relation
PostPosted: Mon Feb 01, 2010 7:23 am 
Beginner
Beginner

Joined: Tue Oct 30, 2007 7:57 am
Posts: 47
I have an strange relation among several tables.

There are 3 main tables PERSON, ROLE and CONTEST. Each has an autonumeric PK. Names are IDPERSON, IDROLE, IDCONTEST

Originally the application had a relation table called ROLECONTEST:
PK = IDROLECONTEST
FKs = IDROLE, IDCONTEST
Other data

Another relation table that related ROLECONTEST and PERSON called PERSONROLECONTEST
PK = IDROLECONTEST
FKs = IDROLECONTEST, IDPERSON
Other data

Later I needed to add a direct relation between PERSON and CONTEST, so I created PERSONCONTEST
PK = IDPERSONCONTEST
FKs = IDCONTEST, IDPERSON
Other data

The ideal solution would be to have 3 FKs in PERSONROLECONTEST to PERSON, ROLE and CONTEST. I that case the thing would be easy. But it is an application in production stage with a lot of data, and I prefer not to make such a massive change. Now I have the problem on how to relate PERSONROLECONTEST and PERSONCONTEST in hibernate.

The relation from PERSONROLECONTEST to PERSONCONTEST was easy:

In the mapping of PERSONCONTEST I declared a propeerties tag that comprises 2 columns:

<properties name="personcontest">
<many-to-one name="person" class="Person" column="IDPERSON" lazy="proxy" fetch="select" />
<many-to-one name="contest" class="Contest" column="IDCONTEST" lazy="proxy" fetch="select" />
</properties>

And in PERSONROLECONTEST I used a formula for the many-to-one relation:

Code:
<many-to-one name="personContest" class="PersonContest" lazy="proxy" update="false" insert="false" property-ref="personcontest">
   <column name="IDPERSON" />         
   <formula>
      (SELECT rc.IDCONTEST FROM ROLECONTEST rc WHERE rc.IDROLECONTEST = IDROLECONTEST    
   </formula>        
</many-to-one>


The problem is that I have no idea on how to make the inverse relation. I need to recover a collection of PersonRoleContest (A set is preferred) using a formula, but I saw no way on how to do that. I know how to use formulas on calculated fields, but not to recover a set.

As a summary, I need to map the relation mapped with >>>>>>>. I have a PERSONCONTEST, and need to get a set of ROLEPERSONCONTEST. The query would be

SELECT rpc.IDROLEPERSONCONTEST
FROM ROLEPERSONCONTEST rpc
LEFT JOIN ROLECONTEST rc ON rc.IDROLECONTEST = rpc.IDROLECONTEST
WHERE rpc.IDPERSON = IDPERSON AND rc.IDCONTEST = IDCONTEST

Code:
                                                          IDROLEPERSONCONTEST (PK)
IDROLECONTEST(PK) >>>>>>>>>IDROLECONTEST (FK)             IDPERSONCONTEST (PK)
IDROLE (FK)                IDPERSON (FK) <<<<<<<<<<<<<<<< IDPERSON (FK)
IDCONTEST (FK) <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< IDCONTEST (FK)



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:
cron
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.