我有两个持久类Comment
和Vote
。 Comment
可以与许多Vote
相关联,并且它们之间存在@OneToMany
- @ManyToOne
关系,这些关系正常运行。我想要做的是按大多数赞成对评论进行排序。如果Vote
列为Vote.up
,则1
为upvote;如果Vote.up
为0
,则select c from Comment c
order by (
(select count(*) from c.votes v where v.up = 1)
- (select count(*) from c.votes v where v.up = 0)
) desc
为downvote。我试图找出他们的不同之处
到目前为止,这是我的HQL,但它无效
// resize the image
switch (draggingResizer) {
case 0:
//top-left
imageX = mouseX;
imageWidth = imageRight - mouseX;
imageY = mouseY;
imageHeight = imageBottom - mouseY;
break;
case 1:
//top-right
imageY = mouseY;
imageWidth = mouseX - imageX;
imageHeight = imageWidth/ratio; //imageBottom - mouseY;
break;
case 2:
//bottom-right
imageWidth = mouseX - imageX;
imageHeight = mouseY - imageY;
break;
case 3:
//bottom-left
imageX = mouseX;
imageWidth = imageRight - mouseX;
imageHeight = mouseY - imageY;
break;
}
有办法做到这一点吗?
答案 0 :(得分:1)
HQL不支持这样的语法,因此您必须使用本机查询:
List<Comment> comments = (List<Comment>) session.createSQLQuery(
"select * " +
"from Comment " +
"where id in ( " +
" select comment_id " +
" from ( " +
" select " +
" c.id as comment_id, " +
" SUM(CASE WHEN v.up=1 THEN 1 ELSE -1 END) AS vote_count " +
" from Comment c " +
" left join Votes v on c.id = v.comment_id " +
" group by comment_id " +
" order by vote_count desc " +
" ) c_v " +
") c_id"
).addEntity(Comment.class).list();
答案 1 :(得分:0)
看看这个SQL示例:
FROM Employee E WHERE E.id > 10 " +
"ORDER BY E.firstName DESC, E.salary DESC
它是一个hql语句的例子。
你确定你的句子工作正常吗?