I have the following JPQL query in a Spring Data Repository:
public interface CarRepository extends Repository<Car, Integer> {
@Query("select distinct c.model from Car c where c.id in :ids")
Set<Model> findDistinctModelByIdIn(@Param("ids") Set<Integer> ids, Sort sort);
}
A client calls the query as follows (which is exposed via Spring Data REST):
http://localhost:8080/api/cars/search/findDistinctModelByIdIn?ids=1,33,55,43&sort=model.name,desc
However, the results are returned unsorted. How can I sort based on the client sort request parameter?
Does Spring only sort on the domain type the repository manages (e.g., only Car not Model)?
Update
Here is my domain model:
@Entity
@Data
public class Car {
@Id
private Long id;
@ManyToOne
private Model model;
}
@Entity
@Data
public class Model {
@Id
private Long id;
private String name;
}
Update
After turning on trace for org.springframework.web, I found the following:
2023-02-09T12:20:16.315-06:00 TRACE 21812 --- [io-9006-exec-10] o.s.web.method.HandlerMethod : Arguments: [org.springframework.data.rest.webmvc.RootResourceInformation@6e3e0c99, {ids=[33283,37901], sort=[model.name,desc]}, findDistinctModelByIdIn, DefaultedPageable(pageable=Page request [number: 0, size 20, sort: UNSORTED], isDefault=true), UNSORTED, org.springframework.data.rest.webmvc.PersistentEntityResourceAssembler...
However, when using @Yuriy-Tsarkov project, the following is logged:
2023-02-09T12:16:17.818-06:00 TRACE 22460 --- [nio-8097-exec-1] o.s.web.method.HandlerMethod : Arguments: [org.springframework.data.rest.webmvc.RootResourceInformation@3e78567e, {ids=[33283,37901], sort=[model.name,desc]}, findDistinctModelByIdIn, DefaultedPageable(pageable=Page request [number: 0, size 20, sort: model.name: DESC], isDefault=false), model.name: DESC, org.springframework.data.rest.webmvc.PersistentEntityResourceAssembler...
So, Spring is perceiving some difference even though I'm using the exact same version of dependencies and my code & config from what I can tell is the same.