User has these :
@ManyToMany(mappedBy = "votedDownBy")
private Set<Foo> votedDown =new HashSet<Foo>();
@ManyToMany(mappedBy = "favouritedBy")
private Set<Foo> favourites = new HashSet<Foo>();
The corresponding Foo has these :
@ManyToMany
private Set<User> votedDownBy = new HashSet<User>();
@ManyToMany
private Set<User> favouritedBy = new HashSet<User>();
When deploying I am getting no default value provided for votedDownBy_id ... and I don't know why ?
@ManyToMany
mappings are done using join tables. In your case, there must be a Foo_User
table that has these mappings. ie, a Foo_id
and a User_id
column. (Substitute Foo_id
and User_id
with the respective primary keys. Also substitute Foo_User
with Table1_Table2
where Table1
is the table for Foo
and Table2
for User
)
You can override these defaults using the @JoinTable
annotation:
@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>();
and
@ManyToMany(mappedBy = "votedDown")
private Set<User> votedDownBy = new HashSet<User>();
@ManyToMany(mappedBy = "favorites")
private Set<User> favouritedBy = new HashSet<User>();
You can also put the mappedBy
annotation in User
and the JoinTable
in Foo
since this is a symmetric bidirectional relationship.
Just adding join table to each mapping fixed it, foo class now looks like this :
@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>();
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.