SqlResultSetMapping本机SQL查询和One2Many关系

时间:2015-03-26 17:26:27

标签: mysql hibernate jpa hql jpql

是否可以借助SqlResultSetMappingentityManager.createNativeQuery从两个不同的表中获取One2Many关系的对象?

例如

@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;
}

查询:

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

基于 native sql 查询我需要获取带有注释的帖子对象。

我的意思是 - 因此我需要收到帖子列表,此列表的每个帖子都已填充了相应的评论。

JPA有可能吗?如果是这样,你能举个例子吗?

0 个答案:

没有答案