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