2

I have a ManyToMany relationship between two classes, for instance class Customer and class Item. A customer can buy several items and an item can be bought by different customers. I need to store extra information in this relationship, for example the day when the item was bought. I wonder how is this usually modelled in JPA, cause I'm not sure how to express this in code. Do I have to create a new class to model all the attributes of the relationship and make a manyToMany relationship between the other classes or is a better way to do this?

Thanks

Javi
  • 19,387
  • 30
  • 102
  • 135
  • OK, as I see I need an extra class. I there any naming convention for this type of classes? – Javi May 31 '10 at 11:48

3 Answers3

3

The recommended way is to create a new association class to store the needed attributes, and two one-to-many associations to the two parties involved.

Péter Török
  • 114,404
  • 31
  • 268
  • 329
1

I guess you will indeed have to create a new class for the relationship.

Maurice Perry
  • 32,610
  • 9
  • 70
  • 97
1

Like you said yourself, the correct way is to create a new class with the additional attributes.

KCL
  • 6,733
  • 10
  • 37
  • 43