-->
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 question
PostPosted: Wed Jul 05, 2006 6:34 am 
Regular
Regular

Joined: Mon Mar 06, 2006 6:18 am
Posts: 95
Location: Bern, Switzerland
Hibernate version: 3.1.3

Hello all

i have the following database table definition:
Code:
* KursEdit                                                          * Coach                   -- Stellvertreter 
   * KursAdd            * mapping tabelle                              * Leiter                 /  \   
+-------------+       +---------------+       +-------------+  m:m  +----------+  m:m  +------------+
| TRANSAKTION |<----->| TBERECHTIGUNG |<----->| TTAETIGKEIT |<----->| TPROFIL  |<----->| TBENUTZER  | 
+-------------+       +---------------+       +-------------+       +----------+       +------------+ 
       \                      /                  * Kurs erfassen                          * Angela
        \    +-----------+   /                   * Anwesenheitskontrolle                  * Thomas
         --- | TAKTION   |---
             +-----------+


TBERECHTIGUNG is a mapping table which contains foreign keys to TAKTION, TTAETIGKEIT und TTRANSAKTION. These three foreign keys are also building a composite id.

Code:
CREATE TABLE  "TBERECHTIGUNG" (
    "FK_TTAETIGKEIT_ILAUFNR" NUMBER NOT NULL ENABLE,
   "FK_TTRANSAKTION_ILAUFNR" NUMBER NOT NULL ENABLE,
   "FK_TAKTION_ILAUFNR" NUMBER NOT NULL ENABLE,
   CONSTRAINT "PK_TBERECHTIGUNG" PRIMARY KEY ("FK_TTAETIGKEIT_ILAUFNR", "FK_TTRANSAKTION_ILAUFNR", "FK_TAKTION_ILAUFNR") ENABLE,
   CONSTRAINT "TBERECHTIGUNG_FK" FOREIGN KEY ("FK_TTAETIGKEIT_ILAUFNR")
     REFERENCES  "TTAETIGKEIT" ("ILAUFNUMMER") DEFERRABLE INITIALLY DEFERRED DISABLE,     
   CONSTRAINT "TBERECHTIGUNG_FK2" FOREIGN KEY ("FK_TTRANSAKTION_ILAUFNR")
     REFERENCES  "TTRANSAKTION" ("ILAUFNUMMER") DEFERRABLE INITIALLY DEFERRED DISABLE,   
   CONSTRAINT "TBERECHTIGUNG_FK3" FOREIGN KEY ("FK_TAKTION_ILAUFNR")
     REFERENCES  "TAKTION" ("ILAUFNUMMER") DEFERRABLE INITIALLY DEFERRED DISABLE
);



How do i have to define this mapping???
I already did the mapping for TBENUTZER, TPROFIL and TTAETIGKEIT, there are mapping tables in between with only two foreign-keys, which i specified like that for example (Benutzer.hbm.xml):


Code:
<set name="profile" table="TBENU_PROFIL"  fetch="join" cascade="all"  >
          <key column="FK_TBENU_ILAUFNR" />
          <many-to-many column="FK_TPROFIL_ILAUFNR" class="najsre7.model.Profil" />
       </set>


Any help appreciated...

kind regards
angela


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jul 05, 2006 7:13 am 
Expert
Expert

Joined: Tue Dec 28, 2004 7:02 am
Posts: 573
Location: Toulouse, France
First, you should know that composite-id are harder to use. So, if your schema is not a legacy one (pre-existing) and so you can change it, you should really avoid using composites.

It would save you much work about it.

What I say is written in the reference documentation :
Quote:
There is an alternative <composite-id> declaration to allow access to legacy data with composite keys. We strongly discourage its use for anything else.
(from http://www.hibernate.org/hib_docs/refer ... aration-id)

If you can change it, you will avoid encountering a lot of potential issues and it will simplify your work also a lot.

_________________
Baptiste
PS : please don't forget to give credits below if you found this answer useful :)


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jul 05, 2006 12:15 pm 
Beginner
Beginner

Joined: Wed Sep 21, 2005 11:52 am
Posts: 43
I am no expert on this sort of mapping, but I had a similar situation, so perhaps I can offer my experience. Your mapping depends on what you want your Java objects to look like. If you want TBERECHTIGUNG to be an object in Java then just map it as an entity with a composite key. And map the foreign keys to the other tables as one-to-one or many-to-one elements as appropriate.

I did not want my association table to be a Java object so I chose to join it to the other tables that used it. For instance:

Code:
    <class name="org.egcrc.cfr.normalized.Appearance" extends="org.egcrc.cfr.common.AbstractPerson" lazy="true">

        <join table="PERSON_APP_RELATIONSHIP" inverse="true" optional="true">
            <key column="PERSON_PK"/>
            <many-to-one name="myNormalizedRecord" column="NORM_REC_PK" class="org.egcrc.cfr.normalized.NormalizedRecord" not-null="true"/>

        </join>
    </class>


So here I am joining the PERSON_APP_RELATIONSHIP table to the PERSON (Appearance object in Java) table on the PERSON_PK foreign key so that I can reference the NormalizedRecord directly from the Appearance object .

If you want a more complete example let me know. Look at section 5.1.17 of the PDF Reference (v 3.0.2) for more on join.

Good luck!

Larry


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.