-->
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: bi-di many-to-many: should lookup table have an PK ID col?
PostPosted: Thu Jun 16, 2005 11:34 am 
Newbie

Joined: Thu Jun 16, 2005 10:59 am
Posts: 3
I'm just getting started with Hibernate and have a general design question.

There are people and phone numbers. People can have multiples numbers and multiple people can have the same number.

I have created the following mapping:

Code:
  <class name="Person" table="People">
    <id name="ID" column="ID" type="Guid" access="nosetter.pascalcase-m-underscore" unsaved-value="00000000-0000-0000-0000-000000000000">
      <generator class="guid.comb"/>
    </id>
    <property name="FirstName" type="String" not-null="false" />
    <property name="LastName" type="String" not-null="false" />
    <property name="MiddleInitial" type="Char" not-null="false" />
    <property name="DateOfBirth" type="DateTime" not-null="false" />

    <joined-subclass name="DetailedPerson" table="DetailedPersons" proxy="DetailedPerson" extends="Person">
      <key column="Person" />

      <set name="PhoneNumbers" table="PersonPhoneNumbers">
        <key column="Person" />
        <many-to-many column="PhoneNumber" class="PhoneNumber" />
      </set>
    </joined-subclass>
  </class>

  <class name="PhoneNumber" table="PhoneNumbers">
    <id name="ID" column="ID" type="Guid" access="nosetter.pascalcase-m-underscore" unsaved-value="00000000-0000-0000-0000-000000000000">
      <generator class="guid.comb"/>
    </id>
    <property name="Number" type="string" not-null="false" />
    <property name="Description" type="string" not-null="false" />
   
    <set name="People" table="PersonPhoneNumbers" inverse="true">
      <key column="PhoneNumber" />
      <many-to-many column="Person" class="DetailedPerson" />
    </set>
  </class>


This works fine as long as the lookup table (PersonPhoneNumber) has just the two columns (the person ID and the phone number ID).

There is the mantra that any table, even lookup tables, should have a PK identify column. I tired adding one but my various stabs (rather in the dark) at it have proven fruitless. I won't enumerate the various things I tried as they all failed and are probably just noise.

The example in the hibernate's docs looks like the lookup table does not have a PK id column.

I know I could make the PK a composite of the two columns or I could just leave it as-is.

If I wanted to add a PK column that was given a guid.comb generated ID is that possible? If so - how, and is it even worth doing?

Thanks.

Robert.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jun 16, 2005 12:39 pm 
Expert
Expert

Joined: Fri May 13, 2005 5:56 pm
Posts: 308
Location: Santa Barbara, California, USA
I use surrogate keys in all of my columne, but I use association classes extensively because there is always more information to store with the association than just the association.

in your instance, i think you have to use an <idbag> with a surrogate key.

so PhoneNumber collection might be mapped as:

Code:
<idbag="PhoneNumbers" table="PersonPhoneNumbers">
    <collection-id type="Guid" column="ID">
        <generator class="guid" />
    </collection-id>
    <key column="Person" />
    <many-to-many column="PhoneNumber" class="PhoneNumber" />
</idbag>


Top
 Profile  
 
 Post subject: Thanks
PostPosted: Sun Jun 19, 2005 6:56 pm 
Newbie

Joined: Thu Jun 16, 2005 10:59 am
Posts: 3
Worked like a champ. I decided against the design in the end but am glad to understand how this works.

Thanks.

Robert.


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.