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.  [ 2 posts ] 
Author Message
 Post subject: One to many relationship to composed primary key
PostPosted: Mon Oct 31, 2005 11:52 am 
Beginner
Beginner

Joined: Thu Oct 20, 2005 12:35 pm
Posts: 21
OK, I am trying to map a weak entity set in my mapping files.

I have 2 tables:

User
- id (primary key)
- ...


Type
- type_id
- User_id
Primary key(type_id, User_id)

When I use it like this, I enforce the fact that there is only one type possible per User.

Mapping file of Type:

<hibernate-mapping>

<class name="Type" table="type">
<composite-id name="id" class="TypeKey">
<key-property name="gebruikerId" column="user_ID" type="integer"/>
<key-property name="typeId" column="type_ID" type="string"/>
</composite-id>

<property name="naam" column="naam" type="string" not-null="true" />
<property name="machtigingen" column="machtigingen" type="string" not-null="true" />
<set name="User" inverse="true">
<key column="ID"/>
<one-to-many class="User"/>
</set>
</class>
</hibernate-mapping>

Relevant part of Use mapping:

<class name="User" table="User">
.
.
.
<many-to-one name="type"/>
</class>

But when I try to use this, I always get an error about:
"Foreign key must have same number of columns as the referenced primary key"

I understand why he is saying this, i only make a reference to one of the primary keys, but I have no idea how to solve this. I can't find an example that is comparable to mine. I've saw complex solutions using <key-many-to-one> ... But that all looked very strange.

Any suggestions about how my mapping file should look ?

Thx


Top
 Profile  
 
 Post subject:
PostPosted: Mon Oct 31, 2005 4:12 pm 
Beginner
Beginner

Joined: Tue Oct 18, 2005 3:57 pm
Posts: 48
Location: Los Angeles, CA
A comment and a question--

1) Assuming table TYPE represents user types, you should make TYPE a lookup table (read-only) and put a TYPE_ID in table USER instead of the other way around (your schema).

2) Also, if you have a TYPE_ID in table TYPE, why can't that id alone be a unique id for Type entities? (since you've already named it "type id"...)

Whatever your reason, you can try adding a surrogate key to Type table:
Code:
Type
=========
ID        -- PK,
TYPE_ID,
USER_ID,  -- FK to User
....


Now the mapping should look like:
Code:
<class name="Type" table="TYPE">
    <id name="id" column="ID" type="...">
        <generator type="..."/>
    </id>

    <property ... />
    <property ... />

    <set name="user" inverse="true">
        <key column="USER_ID"/>
        <one-to-many class="User"/>
    </set>
</class>


Of course TYPE_ID now becomes just another property for Type, but if 1) is what you're trying to do, then just get rid of TYPE_ID and you're all set.

jd


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