Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/apache/9.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
如何在JPA中使用mysql的FIELD()函数_Mysql_Function_Jpa_Criteria - Fatal编程技术网

如何在JPA中使用mysql的FIELD()函数

如何在JPA中使用mysql的FIELD()函数,mysql,function,jpa,criteria,Mysql,Function,Jpa,Criteria,我的sql查询类似于 从表格中选择*按字段排序('ID',3,5,2) 如何使用标准生成器在JPA中实现这一点 编辑 List ordList=newarraylist(); 增加(3); 增加(5); 增加(2); 公共列表getOrderBys(CriteriaBuilder cb,根根){ 列表顺序=新的ArrayList() add(cb.function(“FIELD”,Integer.class,root.get(“id”),ordList)); 退货订单 } 上面是我的函数,它给

我的sql查询类似于

从表格中选择*按字段排序('ID',3,5,2)

如何使用标准生成器在JPA中实现这一点

编辑

List ordList=newarraylist();
增加(3);
增加(5);
增加(2);
公共列表getOrderBys(CriteriaBuilder cb,根根){
列表顺序=新的ArrayList()
add(cb.function(“FIELD”,Integer.class,root.get(“id”),ordList));
退货订单
}

上面是我的函数,它给出了订单列表,我只想添加一个与上面sql查询相同的订单。如何在
orders.add()方法中添加/调用该函数?上面的方法给出了错误。

我们可以使用本机查询标志。 例如,

@Query(value="select * from table_name where id IN :id ORDER BY FIELD(id,:id)",nativeQuery = true)
List<Entity Name> getByIds(@Param("id") List<Integer> id);
@Query(value=“select*from table_name,其中id位于:id ORDER BY FIELD(id,:id)”,nativeQuery=true)
List getbyid(@Param(“id”)List id);

对于这种简单的情况,我不会使用
字段
函数,而是按照所述的排序列表对结果进行排序。 例如:

List ordList=newarraylist();
增加(3);
增加(5);
增加(2);
List resultList=query.getResultStream()
.sort(Comparator.comparing(entity->ordList.indexOf(entity.getId()))
.collect(收集器.toList();

正如JPA2.1的所有JPQL文档所说,您可以使用“Function('funcName',args)”调用SQL函数…并失去DB独立性Hi@Akshay,你们解决了你们的问题吗?不,我们可以按照@BillyFrost所说的做,但代码的sakeWant使用JPA非本机查询并没有做。你们还在搜索答案吗?若我得到答案会更好。事实证明这是不可能的。即使我搜索并最终用本机查询进行了调整。更好的跳跃有人会回答的。
@Query(value="select * from table_name where id IN :id ORDER BY FIELD(id,:id)",nativeQuery = true)
List<Entity Name> getByIds(@Param("id") List<Integer> id);
List<Integer> ordList = new ArrayList<Integer>();
ordList.add(3);
ordList.add(5);
ordList.add(2);
List<Entity> resultList = query.getResultStream()
    .sort(Comparator.comparing(entity -> ordList.indexOf(entity.getId())))
    .collect(Collectors.toList();