【发布时间】:2015-05-30 20:34:36
【问题描述】:
是否有可能在SqlResultSetMapping 和entityManager.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 查询,我需要使用 cmets 获取帖子对象。
我的意思是 - 因此我需要接收帖子列表,并且此列表中的每个帖子都已经填充了适当的评论。
JPA 可以吗?如果是这样,你能举个例子吗?
【问题讨论】:
标签: mysql hibernate jpa hql jpql