Advertisement

7. User, Comment and Reply Entity mapping establishment in Hindi

7.  User, Comment and Reply Entity mapping establishment in Hindi In this lecture, we have established the mapping between User, Comment, and Reply.

Since one comment can have many replies so the relationship between comment to reply will be OneToMany.
And since many replies can rely on a single reply so the relationship between the reply and comment will be ManyToOne.

Since one user can reply many to a single comment so the relationship between user to comment will be OneToMany and also a user to reply will be OneToMany.

OneToMany relationships are established by using @OneToMany annotation in JPA and ManyToOne relationships are established by using @ManyToOne annotation in JPA.


-----------------------------------
User.class
/// user to post mapping
@OneToMany(mappedBy = "user", fetch = FetchType.LAZY, cascade = CascadeType.ALL)
private Collection(left angle bracket)Post(right angle bracket) posts = new ArrayList(left angle bracket)Post(right angle bracket)();

////(left angle bracket)Post(right angle bracket)
/// user to comment mapping
@OneToMany(mappedBy = "user", fetch = FetchType.LAZY, cascade = CascadeType.ALL)
private Collection(left angle bracket)Comment(right angle bracket) comments = new ArrayList(left angle bracket)Comment(right angle bracket)();

/// user to reply mapping
@OneToMany(mappedBy = "user", fetch = FetchType.LAZY, cascade = CascadeType.ALL)
private Collection(left angle bracket)Reply(right angle bracket) replies = new ArrayList(left angle bracket)Reply(right angle bracket)();

Post.class

/// post to user mapping
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "user_id")
private User user;

/// post to comment mapping
@OneToMany(mappedBy = "post", cascade = CascadeType.ALL, fetch = FetchType.LAZY)
private Collection(left angle bracket)Comment(right angle bracket) comments = new ArrayList(left angle bracket)Comment(right angle bracket)();

Reply.class
/// reply to comment mapping
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "comment_id")
private Comment comment;

/// reply to user mapping
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "user_id")
private User user;

Note: Don't use cascadeType = CascadeType.ALL in the post to the user, post to the comment, reply to comment, comment to the user and reply to user mapping.

social links:
Facebook page :


do{
subscribe();
} while(!subscribed);
do{
like();
}while(!liked);
do{
comments();
}while(1);

OneToMany mapping in JPA in Hindi,ManyToOne mapping in JPA in Hindi,OneToMany in JPA in Hindi,ManyToOne in JPA in Hindi,

Post a Comment

0 Comments