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: Mapping many-to-many without primary key
PostPosted: Wed Oct 20, 2004 5:16 pm 
Beginner
Beginner

Joined: Fri Jul 30, 2004 2:53 pm
Posts: 33
Location: Washington, DC
I have an Item DB table with a unique PK called id and a required but not unique field called code. I have a Category DB table that has a unique PK called id and a name field. There is a many-to-many relationship between Item and Category which is presented in a table called ITEM_CATEGORY. But here's the catch, the ITEM_CATEGORY table has two columns, ITEM_CODE and CATEGORY_ID. ITEM_CODE relates to the code of the item, not the id, which is the primary key. I assume a mapping like this won't work:

Code:
<class name="Item">
    <id name="id" column="ITEM_ID"/>
   <property name="code" column="ITEM_CODE"/>
   
    <bag name="categories" table="ITEM_CATEGORY">
      <key column="ITEM_CODE"/>
      <many-to-many class="Category" column="CATEGORY_ID"/>
    </bag>
</class>


The reason I assume that is because ITEM_CODE is not related to the PK of Item. For further clarification, here is what the DB tables would look like:

Code:
ITEM

ITEM_ID    ITEM_CODE
-------    ---------
1           ABC
2           XYZ
3           XYZ

CATEGORY

CATEGORY_ID    CATEGORY_NAME
-----------    -------------
1               A
2               B
3               C

ITEM_CATEGORY

ITEM_CODE    CATEGORY_ID
---------    -----------
ABC           1
XYZ           2



So my question, is there a way to map an association like this in hibernate, and be able to get all the items in a category?


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