• <i id='NMENh'><tr id='NMENh'><dt id='NMENh'><q id='NMENh'><span id='NMENh'><b id='NMENh'><form id='NMENh'><ins id='NMENh'></ins><ul id='NMENh'></ul><sub id='NMENh'></sub></form><legend id='NMENh'></legend><bdo id='NMENh'><pre id='NMENh'><center id='NMENh'></center></pre></bdo></b><th id='NMENh'></th></span></q></dt></tr></i><div id='NMENh'><tfoot id='NMENh'></tfoot><dl id='NMENh'><fieldset id='NMENh'></fieldset></dl></div>

    <small id='NMENh'></small><noframes id='NMENh'>

      • <bdo id='NMENh'></bdo><ul id='NMENh'></ul>

    1. <tfoot id='NMENh'></tfoot>

        <legend id='NMENh'><style id='NMENh'><dir id='NMENh'><q id='NMENh'></q></dir></style></legend>

        如何保持“in"中提供的订单Spring Data JPA 或 Hibernate 中的子句

        时间:2024-08-23

          <i id='nWhOp'><tr id='nWhOp'><dt id='nWhOp'><q id='nWhOp'><span id='nWhOp'><b id='nWhOp'><form id='nWhOp'><ins id='nWhOp'></ins><ul id='nWhOp'></ul><sub id='nWhOp'></sub></form><legend id='nWhOp'></legend><bdo id='nWhOp'><pre id='nWhOp'><center id='nWhOp'></center></pre></bdo></b><th id='nWhOp'></th></span></q></dt></tr></i><div id='nWhOp'><tfoot id='nWhOp'></tfoot><dl id='nWhOp'><fieldset id='nWhOp'></fieldset></dl></div>
          • <small id='nWhOp'></small><noframes id='nWhOp'>

                  <tbody id='nWhOp'></tbody>
                <legend id='nWhOp'><style id='nWhOp'><dir id='nWhOp'><q id='nWhOp'></q></dir></style></legend>
                  <bdo id='nWhOp'></bdo><ul id='nWhOp'></ul>
                • <tfoot id='nWhOp'></tfoot>
                • 本文介绍了如何保持“in"中提供的订单Spring Data JPA 或 Hibernate 中的子句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我有一个非常简单的查询,它根据in"子句检索值.作为in"参数出现的列表被适当地排序.

                  I have a pretty simple query which retrieves values base on "in" clause. List that comes as "in" argument is appropriately sorted.

                  查询:

                  @Query(value = "select i from ItemEntity i where i.secondaryId in :ids")
                  List<ItemEntity> itemsIn(@Param("ids") List<UUID> ids, Pageable pageable);
                  

                  我需要以与 List<UUID> 相同的方式对结果进行排序.ids,是否有可能在没有普通 sql 的情况下实现这一点,但只能在 Spring Data 和/或 Hibernate 的帮助下实现.

                  I need results to be ordered the same way as List<UUID> ids, is it possible to achieve this without plain sql but only with the help of Spring Data and/or Hibernate.

                  推荐答案

                  你也可以通过 JPA 做到这一点,但你必须按照你想要的顺序创建一个逗号分隔的 id 列表.在您的情况下,您可以保持相同的顺序.

                  You can do that by JPA too but you will have to create a comma separated list of ids in the order you want. In your case you can keep same order.

                  @Query(value = "select i from ItemEntity i where i.secondaryId in :ids 
                         order by FIND_IN_SET(i.secondaryId, :idStr)")
                  List<ItemEntity> itemsIn(@Param("ids") List<UUID> ids, @Param("idStr") String idStr);
                  

                  要创建逗号分隔列表,您可以使用 java 8 流:

                  To create comma separated list you can java 8 stream:

                  ids.stream().map(Object::toString).collect(Collectors.joining(","));
                  

                  例子:

                  SELECT id FROM User WHERE id in (2,3,1) 
                  ORDER BY FIND_IN_SET(id,"2,3,1");
                  

                  结果:

                  +----+
                  | id |
                  +----+
                  |  2 |
                  |  3 |
                  |  1 |
                  +----+
                  

                  还有一种使用 JPQL 的替代方法:

                  There is also one alternative using JPQL:

                  You can use ,,FIELD'' instead of ,,FIND_IN_SET ''.
                  

                  您可以在此处找到示例:https://stackoverflow.com/a/65943906/15101302

                  You can find the example here: https://stackoverflow.com/a/65943906/15101302

                  这篇关于如何保持“in"中提供的订单Spring Data JPA 或 Hibernate 中的子句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:Spring Data JPA - 在没有 @Transactional 的情况下获取延迟加载的集合 下一篇:最新和旧版本冲突的两个 Maven 依赖关系

                  相关文章

                  <legend id='ZagvQ'><style id='ZagvQ'><dir id='ZagvQ'><q id='ZagvQ'></q></dir></style></legend>

                  <small id='ZagvQ'></small><noframes id='ZagvQ'>

                  1. <tfoot id='ZagvQ'></tfoot>
                    1. <i id='ZagvQ'><tr id='ZagvQ'><dt id='ZagvQ'><q id='ZagvQ'><span id='ZagvQ'><b id='ZagvQ'><form id='ZagvQ'><ins id='ZagvQ'></ins><ul id='ZagvQ'></ul><sub id='ZagvQ'></sub></form><legend id='ZagvQ'></legend><bdo id='ZagvQ'><pre id='ZagvQ'><center id='ZagvQ'></center></pre></bdo></b><th id='ZagvQ'></th></span></q></dt></tr></i><div id='ZagvQ'><tfoot id='ZagvQ'></tfoot><dl id='ZagvQ'><fieldset id='ZagvQ'></fieldset></dl></div>

                        <bdo id='ZagvQ'></bdo><ul id='ZagvQ'></ul>