Is it possible with help of SqlResultSetMapping
and entityManager.createNativeQuery
fetch object with One2Many
relations from two different tables ?
For example
@Entity
@Table(name = "posts")
public class Post {
@OneToMany(mappedBy = "post")
private List<Comment> comments;
}
@Entity
@Table(name = "comments")
public class Comment {
@ManyToOne(optional = false)
@JoinColumn(name = "post_id", referencedColumnName = "post_id")
private Post post;
}
Query:
select p.*, c.* from posts p left join (
select * from comments where content like "%test%" order by last_edited limit 0, 3)
as c on p.post_id = c.post_id
based on native sql query I need to fetch posts objects with a comments.
I mean - as a result I need to receive List of Posts and each post of this list is already populated with an appropriate Comments.
Is it possible with JPA ? If so, could you please show an example ?