【问题标题】:Can you order hql results based on matching restrictions?您可以根据匹配限制订购 hql 结果吗?
【发布时间】:2009-02-05 22:09:39
【问题描述】:

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

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

对狗的描述有不同的排名,例如:“small”为 5,“furry”为 2,“friendly”为 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”),但我想我一直在叫错树。

【问题讨论】:

    标签: hibernate hql


    【解决方案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
    

    【讨论】:

      【解决方案2】:

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

      from Dog d where d.dogMetadata.desc in ('furry','small','whatever') order by d.dogMetadata.rank 
      

      【讨论】:

      • 我不知道 hql in 子句。这很有帮助,谢谢。您的答案(经过一些调整)/几乎/有效;我需要让匹配多个 desc 的 Dogs 在列表中排名更高(基于排名总和)。
      猜你喜欢
      • 1970-01-01
      • 2021-04-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-05-10
      • 1970-01-01
      • 2019-09-13
      • 1970-01-01
      相关资源
      最近更新 更多