2

我有一个对象Dog,其中包含一组DogMetadata

每个 DogMetadata 都有两个值:一个 String(“ desc ”)和一个 int(“ rank ”)。

对狗的描述有不同的排名,例如:“小”是 5,“毛茸茸”是 2,“友好”是 9,“dalmation”是 11,“mutt”是 22。

我需要根据它们的任何desc 值搜索狗(例如,查找“毛茸茸”或“小型”狗)。

此查询返回匹配的狗,但它们没有任何顺序。

select distinct Dog as d
left join d.dogMetadata as dMeta
where ( (dMeta.desc = 'furry') OR (dMeta.desc = 'small') )

如何按任何匹配 DogMetadatas 的总“排名”值对匹配的 Dog 对象列表进行排序?

我整天都在像狗一样工作(尝试“Group By”和“Order By”),但我想我一直在叫错树。

4

2 回答 2

1

此查询不起作用:

select d from Dog as d
left join fetch d.dogMetadata as dMeta
where ( (dMeta.desc = 'furry') OR (dMeta.desc = 'small') )
group by d
order by sum(dMeta.rank) desc

由于这个错误: http://opensource.atlassian.com/projects/hibernate/browse/HHH-1615?focusedCommentId= 24530#action_24530

我只通过请求狗 ID 来解决这个限制:

select d.id from Dog as d
left join fetch d.dogMetadata as dMeta
where ( (dMeta.desc = 'furry') OR (dMeta.desc = 'small') )
group by d.id
order by sum(dMeta.rank) desc
于 2009-02-06T17:52:49.380 回答
0

只是从我的头顶(没有在 HQL 中实际验证它)

from Dog d where d.dogMetadata.desc in ('furry','small','whatever') order by d.dogMetadata.rank 
于 2009-02-05T23:34:39.847 回答