0

用户有这些:

@ManyToMany(mappedBy = "votedDownBy")
private Set<Foo> votedDown =new HashSet<Foo>();

@ManyToMany(mappedBy = "favouritedBy")
private Set<Foo> favourites = new HashSet<Foo>();

对应的 Foo 有这些:

@ManyToMany
private Set<User> votedDownBy = new HashSet<User>();

@ManyToMany
private Set<User> favouritedBy = new HashSet<User>();

部署时,我没有为 votedDownBy_id 提供默认值……我不知道为什么?

4

2 回答 2

0

@ManyToMany映射是使用连接表完成的。在您的情况下,必须有一个Foo_User具有这些映射的表。即一个Foo_id和一个User_id列。(用各自的主键替换Foo_id和。也用where是 table和for )User_idFoo_UserTable1_Table2Table1FooTable2User

@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注释放入UserJoinTable放入,Foo因为这是对称的双向关系。

于 2013-01-28T17:18:19.223 回答
0

只需将连接表添加到每个映射修复它, foo 类现在看起来像这样:

@ManyToMany
@JoinTable(name="user_foo_vote_down")
private Set<User> votedDownBy = new HashSet<User>();

@ManyToMany
@JoinTable(name="user_foo_favourite")
private Set<User> favouritedBy = new HashSet<User>();
于 2013-01-29T09:34:07.927 回答