开发者

JPQL to query entity connected to another by @OneToMany

开发者 https://www.devze.com 2023-01-18 06:16 出处:网络
Has: class Container { @Id @Column(name=\"id\") protected long id; @OneToMany @JoinColumn(name=\"container_id\", nullable=false)

Has:

class Container {

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

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

}

and

class 开发者_如何学编程Content {

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

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

}

What JPQL query will get Content entities that are in Container with certain id and with certain linkId?


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 

But it would be easier to make the association bidirectional:

SELECT c from Content c 
WHERE c.container.id = :containerId AND c.linkId = :linkId

And the generated SQL looks better (and more efficient).

0

精彩评论

暂无评论...
验证码 换一张
取 消