This question already has an answer here:
I have a pretty simple query which retrieves values base on "in" clause. List that comes as "in" argument is appropriately sorted.
Query :
@Query(value = "select i from ItemEntity i where i.secondaryId in :ids")
List<ItemEntity> itemsIn(@Param("ids") List<UUID> ids, Pageable pageable);
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
.
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.
To create comma separated list you can java 8 stream:
Example:
Result: