3

I am looking for a way to have a @OneToMany relation between two tables and having extra attributes in the join table and I am not able to find much useful examples around. Sorry if this sounds lame but can any suggest me a good way for it.

yousafsajjad
  • 973
  • 2
  • 18
  • 34
  • you should not use join table for 1 to m relation – gstackoverflow Jan 23 '14 at 20:59
  • 1
    Can you provide some more detail? What do your tables look like? What would you like your join table to look like? What have you tried? – woemler Jan 23 '14 at 20:59
  • you mean many to many relation. If so here is a way http://stackoverflow.com/questions/10294338/many-to-many-hibernate-mapping-for-additional-property-in-the-join-table – RP- Jan 23 '14 at 21:01

1 Answers1

2

If you have additional columns in the join table, it's not a join table anymore, and you need a way to get and set values in these columns. So the answer is simple: the table needs to be mapped as an entity.

For example, let's say you have a Person entity and an Address entity. And the person has several addresses. Now let's say each address must be categorized: home address, professional address, etc. To be able to map the category of the address, you need an entity:

Person 1 ---> N CategorizedAddress 1 ---> 1 Address

And CategorizedAddress would have a property of type Address, and a property containing the category.

JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255