-->
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.  [ 6 posts ] 
Author Message
 Post subject: Mapping Strategy
PostPosted: Fri Mar 21, 2008 2:56 pm 
Beginner
Beginner

Joined: Wed Mar 05, 2008 10:32 am
Posts: 48
I cant wrap my brain around this one.. didnt eat breakfast might be the cause.

I want to create a messaging system between users.
2 users are related to 1 message
each user can have many message

how do i map this relationship?
is it
1) a user entity has a many to one relationship with a message entity
2) a user has a collection of message entities
3) there is a manytomany relationship between the users and messages

I have both the user and the message as an entity. I figured since its a shared resource (between 2 users) it should not be embeddable... please advise if i'm wrong.


thx


Top
 Profile  
 
 Post subject: Re: Mapping Strategy
PostPosted: Fri Mar 21, 2008 3:00 pm 
Expert
Expert

Joined: Wed Apr 11, 2007 11:39 am
Posts: 735
Location: Montreal, QC
If as per requirement there is only two users involved in a message then each message has two many-to-one relation with the user entity and each user entity has one one-to-many relation with messages.



Farzad-


Top
 Profile  
 
 Post subject:
PostPosted: Mon Mar 24, 2008 4:09 pm 
Beginner
Beginner

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


Top
 Profile  
 
 Post subject:
PostPosted: Mon Mar 24, 2008 8:16 pm 
Expert
Expert

Joined: Wed Apr 11, 2007 11:39 am
Posts: 735
Location: Montreal, QC
You need to have two message sets for a user. One holding outgoing messages and on for incoming messages. You could also have a method that combines these two but it should be read only.



Farzad-


Top
 Profile  
 
 Post subject:
PostPosted: Mon Apr 07, 2008 6:22 pm 
Beginner
Beginner

Joined: Wed Mar 05, 2008 10:32 am
Posts: 48
Quote:
You need to have two message sets for a user. One holding outgoing messages and on for incoming messages.


This to me doesnt seem like a feasible way to do this. That seems a lot more work for the server if it has to load extra things. Then take into account i have to distinguish between whats outgoing and incoming.

So other than this is there no other way for 2 users to share an email message? I think we can call it a conversation.. if that will help in creating the schema.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Apr 08, 2008 12:18 am 
Newbie

Joined: Wed Mar 28, 2007 1:05 am
Posts: 8
Hi,

I think Farzad has given correct solution. In any conversation you need to maintain sender and receiver for a message, and send and received messages separately for a user.


Cheers
Deepak


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 6 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.