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?