• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Many-To-Many relationship probs...

 
taltun taltun
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I have problem with solving below solution which I have spent too much time:

An User domain object need to have 2 collections.

1. One representing a collection of Document objects that the user have created/is owner of.
Example:
private Set<Document> myDocumentsSentToOther = new HashSet<Document>()

2. Another representing a collection of Document objects that other users have created/owner of but sent to this User.
Example:
private Set<Document> othersDocumentsSentToMe = new HashSet<Document>()

public class User {
private Long id;
private Set<Document> myDocuments = new HashSet<Document>()
private Set<Document> othersDocuments = new HashSet<Document>()
....
}

public class Document {
private Long id;
private User owner;
....
}

As I see it is a Many-To-Many relationship because an User can create and send many Document to other users and receive many Documents sent by other users. Therefore, I need a way to get both collections, those I have sent to others and those other users have among other thing sent to me and I need to seperate them in two collections.

How do I code this in JPA ?

Please, provide annotation based code examples of above two domain objects.

Please provide code examples.

Thank you very much...

-taltu
 
Vikas Kapoor
Ranch Hand
Posts: 1374
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Taltun,

Welcome to JavaRanch!

Please keep following points in mind
Kindly adhere JavaRanch Naming Policy. Refer http://www.javaranch.com/name.jsp for more information.
Use code tags to enclose your code. Refer http://faq.javaranch.com/java/UseCodeTags It gives ease in reading your post.

Regarding your problem, I haven't worked with JPA but it seems plain many to many relationship. Refer http://www.coderanch.com/t/218842/Object-Relational-Mapping/java/many-many-jpa-saving-example . That says search first http://faq.javaranch.com/java/SearchFirst

You are new to Ranch so you might want to refer http://faq.javaranch.com/java/HowToAskQuestionsOnJavaRanch



 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic