JPQL查询@OneToMany连接到另一个实体的实体

Tim*_*kov 1 jpa jpql

具有:

class Container {

    @Id
    @Column(name="id")
    protected long id;

    @OneToMany
    @JoinColumn(name="container_id", nullable=false)
    protected Collection<Content> contents = new ArrayList<Content>();

}
Run Code Online (Sandbox Code Playgroud)

class Content {

    @Id
    @Column(name="id")
    protected long id;

    @Column(name="link_id")
    protected long linkId;

}
Run Code Online (Sandbox Code Playgroud)

什么JPQL查询将获取具有特定ID和某些linkId的Container中的内容实体?

Pas*_*ent 7

Without making the association bidirectional, you could do:

SELECT c 
FROM Content c, Container container 
WHERE c MEMBER OF container.contents AND c.linkId = :linkId AND container.id = :containerId 
Run Code Online (Sandbox Code Playgroud)

但是使关联双向更容易:

SELECT c from Content c 
WHERE c.container.id = :containerId AND c.linkId = :linkId
Run Code Online (Sandbox Code Playgroud)

并且生成的SQL看起来更好(并且更有效).