Issue
I want to avoid "Bidirectional @OneToMany
" association due to:
- It cannot limit the size of a
@OneToMany
- I need to do pagination.
For this I used href="https://vladmihalcea.com/the-best-way-to-map-a-onetomany-association-with-jpa-and-hibernate/" rel="nofollow noreferrer">this tutorial which described "Just @ManyToOne
" association, but, unfortunately, it gives only one line of code related to this:
List<PostComment> comments = entityManager.createQuery(
"select pc " +
"from PostComment pc " +
"where pc.post.id = :postId", PostComment.class)
.setParameter( "postId", 1L )
.getResultList();
So, I have many questions:
Where exactly should I use this line?
How and where should I obtain EntityManager? Exactly in the entity? Is it a good solution?
How can I avoid using
EntityManager
? I already looked at this and other questions, but unfortunately they didn't help me.I have
Post
as a parent andComment
as a child entity. One post can has many comments. Code:
If I use this in the Comment
:
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "post_id")
private Post post;
And this in the Post
:
private Set<Comment> comments;
So, I deleted @OneToMany
as above-mentioned tutorial said, I got:
MappingException: Could not determine type for: java.util.List, at table: post, for columns: [org.hibernate.mapping.Column(comments)]
So, how can I use "Just @ManyToOne
" association(or something else convenient) to get control of the size and pagination for comments
?
Solution
I found not perfect, but the most correct solution for me.
Post:
@Entity(name = "Post")
public class Post {
//...
@Transient
private List<PostComment> comments;
public void addComment(PostComment comment) {
comments.add(comment);
comment.setPost(this);
}
public void removeComment(PostComment comment) {
comments.remove(comment);
comment.setPost(null);
}
public void clearComments(){
comments.clear();
}
public List<PostComment> getComments() {
return comments;
}
public void setComments(List<PostComment> comments) {
this.comments = comments;
}
}
PostComment:
@Entity(name = "PostComment")
public class PostComment {
//...
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "post_id")
private Post post;
public Post getPost() {
return post;
}
public void setPost(Post post) {
this.post = post;
}
}
PostCommentServiceImpl:
@Service
public class PostCommentServiceImpl {
@Autowired
private PostCommentRepository repository;
//...
public void setCommentsInPost(Post post, int first, int size){
Pageable pageRequest = new PageRequest(first, size);
Page<PostComment> pageOfPostComment = repository.findByPostId(post.getId(), pageRequest);
post.setComments(pageOfPostComment.getContent());
}
}
Controller:
@Controller
public class PostController {
@Autowired
private PostCommentService postCommentService;
@Autowired
private PostService postService;
//...
@RequestMapping(value = "/something", method = RequestMethod.GET)
public void foo() {
Post post = postService.findById(1L);
postCommentService.setCommentsInPost(post,0,10);
//...
}
//...
}
Answered By - konstantin_doncov
Answer Checked By - Timothy Miller (JavaFixing Admin)