如何使用三个对象进行最佳的休眠查询?

发布于 2024-09-25 16:15:03 字数 166 浏览 0 评论 0原文

我有一个疑问,但我不知道如何使其最佳。我有三个对象。

  • SCP
  • 问题:它有一个指向 SCP (SCP_id) 的字段
  • 答案:它有一个指向问题 (question_id) 的字段

我如何进行查询来计算给定 SCP 内的答案数量

I have a query but I don't know how to make it optimal. I have three objects.

  • SCP
  • Question : it has a field that points SCP (SCP_id)
  • Answer : it has a field that points Question (question_id)

How can I make a query that counts the number of answer within a given SCP

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

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

发布评论

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

评论(1

独守阴晴ぅ圆缺 2024-10-02 16:15:03

怎么样:

select count(a) 
from Answer a 
where a.question.scp.id = :id

它会生成以下 SQL:

select
  count(answer0_.id) as col_0_0_ 
 from
  Answer answer0_,
  Question question1_ 
 where
  answer0_.question_id=question1_.id 
  and question1_.scp_id=?

看起来相当高效。

What about this:

select count(a) 
from Answer a 
where a.question.scp.id = :id

Which generates the following SQL:

select
  count(answer0_.id) as col_0_0_ 
 from
  Answer answer0_,
  Question question1_ 
 where
  answer0_.question_id=question1_.id 
  and question1_.scp_id=?

Seems pretty efficient.

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