• <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 中的子句

        How to keep order provided in quot;inquot; clause in Spring Data JPA or Hibernate(如何保持“in中提供的订单Spring Data JPA 或 Hibernate 中的子句)

          <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 中的子句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  How to send data to COM PORT using JAVA?(如何使用 JAVA 向 COM PORT 发送数据?)
                  How to make a report page direction to change to quot;rtlquot;?(如何使报表页面方向更改为“rtl?)
                  Use cyrillic .properties file in eclipse project(在 Eclipse 项目中使用西里尔文 .properties 文件)
                  Is there any way to detect an RTL language in Java?(有没有办法在 Java 中检测 RTL 语言?)
                  How to load resource bundle messages from DB in Java?(如何在 Java 中从 DB 加载资源包消息?)
                  How do I change the default locale settings in Java to make them consistent?(如何更改 Java 中的默认语言环境设置以使其保持一致?)

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

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

                    <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>
                          <tbody id='ZagvQ'></tbody>

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