tags:

views:

37

answers:

3

Hi,

I have a hibernate mapping where I am creating a join table using the <bag> tags. How do I ensure uniqueness of the combination of keys that go into the join table ?

Sample here:

<bag name="PersonStuffs" table="person_x_stuff" cascade="none" >
   <key column="person_id" not-null="true" />
   <many-to-many class="com.sample.Person" column="stuff_id" />  
</bag>
A: 

You make the combination of those two keys the (composite) primary key of the join table. See this example.

cletus
I had posted a vague question before. Have added sample code. Thanks !
Icarus
A: 

For now, the constraints are added separately. But if there is a solution, please let me know.

Icarus
A: 

Not sure I understand your question fully, but if you want to ensure that (person_id,stuff_id) is unique, you may want to use <set>, and <one-to-many>.

Maurice Perry