有:
class Container {
@Id
@Column(name="id")
protected long id;
@OneToMany
@JoinColumn(name="container_id", nullable=false)
protected Collection<Content> contents = new ArrayList<Content>();
}
和
class Content {
@Id
@Column(name="id")
protected long id;
@Column(name="link_id")
protected long linkId;
}
哪些JPQL查询将获取具有特定ID且具有特定linkId的Container中的内容实体?
答案 0 :(得分:7)
如果不进行双向关联,您可以这样做:
SELECT c
FROM Content c, Container container
WHERE c MEMBER OF container.contents AND c.linkId = :linkId AND container.id = :containerId
但是使关联双向化会更容易:
SELECT c from Content c
WHERE c.container.id = :containerId AND c.linkId = :linkId
生成的SQL看起来更好(也更有效)。