使用Hibernate / JPA按给定顺序选择一组对象的最佳方式是什么? [英] What is the best way of selecting a set of objects by given ids in given order with Hibernate/JPA

查看:115
本文介绍了使用Hibernate / JPA按给定顺序选择一组对象的最佳方式是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用给定的ID选择一些对象,但也按给定顺序选择,例如:

I want to select a number of objects with given ids but also in given order, something like:

<named-query name="getQuestionsByIds">
    <query><![CDATA[from Question q where q.id in (:ids)]]></query>
</named-query>

但是按照与参数中的id相同的方式排序。

But ordered the same way as ids in the parameter.

例如在MySQL中,它可以这样做:

E.g. in mysql it can be done like this:

SELECT * FROM table ORDER BY FIELD( id, 23, 234, 543, 23 )

最好的方法是什么?

What is the best way?

推荐答案

Hibernate会保留它不知道的函数,并在写入时将它们传递给SQL,因此,假设您使用的是MySQL,您是否尝试使用' ORDER BY FIELD'子句?如果...

Hibernate keeps the functions it doesn't know and pass them to SQL as they were written, so, assuming that you're using MySQL, have you tried to write your HQL with the 'ORDER BY FIELD' clause? Something like...

select q from Question q where q.id in (:ids) ORDER BY FIELD(id, :ids)

这篇关于使用Hibernate / JPA按给定顺序选择一组对象的最佳方式是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆