Search code examples
springjpaormmappingentity

Need help regarding JPA entity mapping


I'm fairly new to ORM. I'm having trouble deciding how exactly I should map the following entities.

  1. DiscussionThread
  2. Post
  3. User
  4. AnonymousUser

DiscussionThread would be something similar to the ones we see in bulletin boards online. It would contain a list of Post which would be posted by User. However, I do not want the User to reveal his/her identity while posting in the DiscussionThread. In order to achieve that I created a list of proxy usernames denoted by the entity AnonymousUser. Thus, whenever a User decides to make a Post in a DiscussionThread, he would be posting as an AnonymousUser. Any further Post made by the same User in that DiscussionThread would be linked to the same AnonymousUser.The User will have different AnonymousUser names in different DiscussionThreads. An instance of AnonymousUser may be used by two different users on two different threads.

In simpler words, there will be one AnonymousUser for one User in each DiscussionThread.

I have created the following POJO entities, but I'm stuck in how I should map them to each other.

public class AnonymousUser {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;

    private String name;
    private String displayPicture;

    //Not sure how to make relationships here
    private Set<DiscussionThread> discussionThreads;

    private Set<User> users;
}

public class DiscussionThread {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;

    private String title;

    private String description;
}

public class User {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;

    private String email;

    private String username;
}

Any help will be appreciated.

Thank you!


Solution

  • Well, you basically described:

    enter image description here

    Don't know if it's right or not but this is one way you could diagram and think about such problems. This is Chen's database notation in Visio.