@ManyToMany
映射是使用连接表完成的。在您的情况下,必须有一个Foo_User
具有这些映射的表。即一个Foo_id
和一个User_id
列。(用各自的主键替换Foo_id
和。也用where是 table和for )User_id
Foo_User
Table1_Table2
Table1
Foo
Table2
User
@JoinTable
您可以使用注释覆盖这些默认值:
@ManyToMany(mappedBy = "votedDownBy")
@JoinTable(name="User_Foo_Votedown",
joinColumns={@JoinColumn(name="User_ID")},
inverseJoinColumns={@JoinColumn(name="Foo_id")})
private Set<Foo> votedDown =new HashSet<Foo>();
@ManyToMany(mappedBy = "favouritedBy")
@JoinTable(name="User_Foo_Fav",
joinColumns={@JoinColumn(name="User_ID")},
inverseJoinColumns={@JoinColumn(name="Foo_id")})
private Set<Foo> favourites = new HashSet<Foo>();
和
@ManyToMany(mappedBy = "votedDown")
private Set<User> votedDownBy = new HashSet<User>();
@ManyToMany(mappedBy = "favorites")
private Set<User> favouritedBy = new HashSet<User>();
您也可以将mappedBy
注释放入User
和JoinTable
放入,Foo
因为这是对称的双向关系。