I have it partially working but it only works for the first user. Here is what i have, please tell me how i need to change this in order to make the call userx.getMessages() show all the message you are associated with (either sender or reciever).
UserEnt
Code:
private Set<MessageEnt> messages = new HashSet<MessageEnt>();
@OneToMany (cascade = {CascadeType.PERSIST, CascadeType.MERGE }, mappedBy = "from", fetch=FetchType.EAGER)
@org.hibernate.annotations.Cascade(value = org.hibernate.annotations.CascadeType.SAVE_UPDATE)
public Set<MessageEnt> getMessages() {
return messages;
}
public void setMessages(Set<MessageEnt> messages) {
this.messages = messages;
}
public void sendMessage(MessageEnt message){
if (message == null)
throw new IllegalArgumentException("adding null message");
this.messages.add(message);
message.setFrom(this);
message.setTo(message.getTo());
}
public void deleteMessage(MessageEnt message){
if (message == null)
throw new IllegalArgumentException("removing null message");
this.messages.remove(message);
message.setFrom(null);
message.setTo(null);
}
MessageEnt
Code:
private UserEnt from;
private UserEnt to;
private String message;
private String subject;
@ManyToOne
@JoinColumn
@NotNull
public UserEnt getFrom() {
return from;
}
public void setFrom(UserEnt from) {
this.from = from;
}
@ManyToOne
@JoinColumn
@NotNull
public UserEnt getTo() {
return to;
}
public void setTo(UserEnt to) {
this.to = to;
}