开发者

How to make an optimal hibernate query with three objects?

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


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.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜