Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/71.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql Hibernate和Oracle按给定顺序排序_Sql_Oracle_Hibernate - Fatal编程技术网

Sql Hibernate和Oracle按给定顺序排序

Sql Hibernate和Oracle按给定顺序排序,sql,oracle,hibernate,Sql,Oracle,Hibernate,我正在使用一个类似于POST的查询,以获得给定顺序的对象列表 我还有两个环境,一个使用MySQL,另一个使用Oracle。在MySQL中,答案中给出的HQL(为referenece发布)运行良好,但当切换到Oracle时,它会转换为相同的结果SQL,这是无效的 select q from Question q where q.id in (:ids) ORDER BY FIELD(id, :ids) 因此,我想扩展Oracle10gDialogue,并将其翻译成与Oracle兼容的SQL:

我正在使用一个类似于POST的查询,以获得给定顺序的对象列表

我还有两个环境,一个使用MySQL,另一个使用Oracle。在MySQL中,答案中给出的HQL(为referenece发布)运行良好,但当切换到Oracle时,它会转换为相同的结果SQL,这是无效的

select q from Question q where q.id in (:ids) ORDER BY FIELD(id, :ids)
因此,我想扩展Oracle10gDialogue,并将其翻译成与Oracle兼容的SQL:

 select q.id            
        from
            Question q
        where
            q.id in (
                1701, 1698, 1264
            ) 
        order by
            decode(q.id,
            1701, 1, 1698, 2, 1264 , 3 )
但我不知道如何定义我的函数。在处理bitwise和时,我扩展了方言并使用了以下代码:

registerFunction("bwand", new SQLFunctionTemplate(IntegerType.INSTANCE, "BITAND(?1, ?2)"));
这很好,但在本例中,我需要提供的模式是id列表,后跟它的顺序索引,我不知道如何获取索引号

有什么帮助吗?

Oracle、MySQL和(我相信)Hibernate都支持
order by
子句中的
case
语句

所以你可以这样写:

order by (case when q.id = 1701 then 1
               when q.id = 1698 then 2
               when q.id = 12c4 then 3
          end)

如果对象的数量很少,可以用Java进行排序。