• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
  • Tim Cooke
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Liutauras Vilda
  • Rob Spoor
  • Junilu Lacar
  • paul wheaton
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
  • Scott Selikoff
  • Piet Souris
  • Jj Roberts
  • fred rosenberger

Many-To-Many relationship probs...

Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
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.
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...

Ranch Hand
Posts: 1374
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 https://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

pie. tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
    Bookmark Topic Watch Topic
  • New Topic