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

发布于 2024-09-25 13:52:05 字数 477 浏览 0 评论 0原文

有:

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 的内容实体?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

ぇ气 2024-10-02 13:52:05

如果不使关联成为双向,您可以这样做:

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 看起来更好(并且更高效)。

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).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文