I would like to create a Many-To-Many mapping using annotations but my MappedBy column is incorrect. My 2 objects are a UserEnt and an EventEnt. I want the EventEnt to contain a set of invited users to the event. At the same time, I want each user to have a set of events, they have been invited to. Thus calling for an M-M mapping. This is what I currently have and I have tried several column names under the MappedBy property only to have an error pop up.
UserEnt class
Code:
@ManyToMany (cascade = {CascadeType.PERSIST, CascadeType.MERGE })
@org.hibernate.annotations.Cascade(value = org.hibernate.annotations.CascadeType.SAVE_UPDATE)
public Set<UserEnt> getInviteList() {
return inviteList;
}
EventEnt class
Code:
@ManyToMany(mappedBy="users")
public Set<EventEnt> getInviteList() {
return inviteList;
}
My second question is concerning a set of object inside that object. Now I know how to annotate the getter method, but I don’t know how to properly create “add” and “remove” methods for the sets. This is also in the UserEnt class as I want to give every user a “friends” list (Set<UserEnt>). This is what I currently have and I’ve been using the same block (altered for other objects) but on sets that were of a different object.
Code:
@org.hibernate.annotations.CollectionOfElements(fetch=FetchType.LAZY)
public Set<UserEnt> getFriends() {
return friends;
}
public void addFriend(UserEnt friend){
if (friend == null)
throw new IllegalArgumentException("adding null friend");
this.friends.add(friend);
friend.setFriends(this);
}
My hiccup is in the final line of code: friend.setFriends(this) where my setFriends method only accepts a Set<UserEnt> and not just a UserEnt.
Help for either of these would be highly appreciated! Thanks!
P.S. I think the search capability of this forum is broken..