How to present a three-way relationship with JPA?

A user can have several shortcuts and links. The user then associates the label (or more) with the link. How can you imagine a later relationship?

The solution can be many, many user btw relationships and a link with an optional attribute label. http://en.wikibooks.org/wiki/Java_Persistence/ManyToMany#Mapping_a_Join_Table_with_Additional_Columns In this case, the btw ratio of the user and the tag may be better "virtual".

Any alternative I don't see?

PS: I used google bookmarks terminology as it fits my case well.

+3
source share
3

, :

ER . , .

alt text
(source: grussell.org)

                                                   Figure

:

  • - .
  • .

, .

  • .

alt text
(source: grussell.org)

:

  • , .
  • .

: .

+6

class Label {
    ...
    @OneToOne
    private Link link;
}

/

class Link {
    ...
    @OneToOne
    private Label label;
}
+1

Adding a null mark means that the user link will only contain one label for each link. If you need only one shortcut, go with it.

If you need several shortcuts to a link, then enter a third object, for example.

class LinkLabel
{
   @ManyToOne
   Link link;

   @ManyToOne
   Label label;  

   @ManyToOne
   User user;

}
+1
source

Source: https://habr.com/ru/post/1746697/


All Articles