-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 posts ] 
Author Message
 Post subject: ManyToMany mapping and collections questions
PostPosted: Wed Mar 05, 2008 11:00 am 
Beginner
Beginner

Joined: Wed Mar 05, 2008 10:32 am
Posts: 48
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..


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 05, 2008 12:11 pm 
Senior
Senior

Joined: Tue Jul 25, 2006 9:05 am
Posts: 163
Location: Stuttgart/Karlsruhe, Germany
Hi,

I think for you should try the following value for mappedBy

@ManyToMany(mappedBy="inviteList")

Cheers,

Andy

_________________
Rules are only there to be broken


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 05, 2008 3:44 pm 
Beginner
Beginner

Joined: Wed Mar 05, 2008 10:32 am
Posts: 48
I've fixed my M-M problem.. here is the code for others to look at. I also had problems with the defualt lazy fetching, so i had to force it to eager.

UserEnt

Code:
@ManyToMany(cascade={CascadeType.PERSIST, CascadeType.MERGE},   fetch=FetchType.EAGER, mappedBy="inviteList", targetEntity=EventEnt.class)
public Set<EventEnt> getInviteList() {
   return inviteList;
}


EventEnt

Code:
@ManyToMany (targetEntity=com.epixentertainment.model.UserEnt.class, cascade = {CascadeType.PERSIST, CascadeType.MERGE }, fetch=FetchType.EAGER)
@JoinTable(name="event_userinvite", joinColumns={@JoinColumn(name="event_id")},   inverseJoinColumns={@JoinColumn(name="user_id")})
@org.hibernate.annotations.Cascade(value = org.hibernate.annotations.CascadeType.SAVE_UPDATE)
public Set<UserEnt> getInviteList() {
return inviteList;
}




still working on that other one though!


Top
 Profile  
 
 Post subject: CascadeType.SAVE_UPDATE
PostPosted: Thu Mar 06, 2008 3:34 am 
Pro
Pro

Joined: Tue Jun 12, 2007 4:13 am
Posts: 209
Location: Berlin, Germany
pooter8d wrote:
@ManyToMany (targetEntity=com.epixentertainment.model.UserEnt.class, cascade = {CascadeType.PERSIST, CascadeType.MERGE }, fetch=FetchType.EAGER)
@JoinTable(name="event_userinvite", joinColumns={@JoinColumn(name="event_id")}, inverseJoinColumns={@JoinColumn(name="user_id")})
@org.hibernate.annotations.Cascade(value = org.hibernate.annotations.CascadeType.SAVE_UPDATE)
public Set<UserEnt> getInviteList() {
return inviteList;
}

Sorry for my "dummyness": but what exactly does
Code:
org.hibernate.annotations.CascadeType.SAVE_UPDATE)
?

And what is the difference to JPA's
Code:
cascade = {CascadeType.PERSIST, CascadeType.MERGE }
?
Can anyone from the Annotations team answer this?

Carlo


Top
 Profile  
 
 Post subject: setFriends() issue
PostPosted: Fri Mar 07, 2008 11:21 am 
Regular
Regular

Joined: Wed Dec 21, 2005 6:57 pm
Posts: 70
public void addFriend(UserEnt friend){
if (friend == null)
throw new IllegalArgumentException("adding null friend");
this.friends.add(friend);
friend.setFriends(this);
}


try

Code:
friend.getUserEntities().add(this);


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.