Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to represent a 3-way relation with JPA?

A user may have several labels, and links. Then, a user associates a label (or more) to a link. How does one represent the later relationship?

A solution could be a many-to-many relationship btw user and link with the optional attribute label. http://en.wikibooks.org/wiki/Java_Persistence/ManyToMany#Mapping_a_Join_Table_with_Additional_Columns In this case the relationship btw user and label may better be 'virtual'.

Any alternative I'm not seeing?

PS: I've used google bookmarks terminology, as it matches my case quite well.

like image 804
simpatico Avatar asked Feb 27 '23 23:02

simpatico


1 Answers

If my understanding is correct, then you are in the following case:

Replacing ternary relationships

When ternary relationships occurs in an ER model they should always be removed before finishing the model. Sometimes the relationships can be replaced by a series of binary relationships that link pairs of the original ternary relationship.

                            alt text
(source: grussell.org)

                                                   Figure

: A ternary relationship example

  • This can result in the loss of some information - It is no longer clear which sales assistant sold a customer a particular product.
  • Try replacing the ternary relationship with an entity type and a set of binary relationships.

Relationships are usually verbs, so name the new entity type by the relationship verb rewritten as a noun.

  • The relationship sells can become the entity type sale.

                            alt text
(source: grussell.org)

                                                  Figure : Replacing a ternary relationship

  • So a sales assistant can be linked to a specific customer and both of them to the sale of a particular product.
  • This process also works for higher order relationships.

And this would be my suggestion: introduce a new entity.

like image 153
Pascal Thivent Avatar answered Mar 08 '23 13:03

Pascal Thivent