Total Pageviews

2019/01/09

[Spring] How to sort by multiple properties in Spring Data?

Problem
I have a table:

I am using spring data to find all data, and expect to sort by config_type and key columns.
The code snippet looks like:
    List<CommonConfig> findAllByOrderByConfigTypeAndKey();

But I get this exception:
Caused by: org.springframework.data.mapping.PropertyReferenceException: No property andKey found for type String! Traversed path: CommonConfig.configType.
 at org.springframework.data.mapping.PropertyPath.<init>(PropertyPath.java:94) ~[spring-data-commons-2.0.9.RELEASE.jar:2.0.9.RELEASE]
 at org.springframework.data.mapping.PropertyPath.create(PropertyPath.java:358) ~[spring-data-commons-2.0.9.RELEASE.jar:2.0.9.RELEASE]
 at org.springframework.data.mapping.PropertyPath.create(PropertyPath.java:334) ~[spring-data-commons-2.0.9.RELEASE.jar:2.0.9.RELEASE]
 at org.springframework.data.mapping.PropertyPath.create(PropertyPath.java:368) ~[spring-data-commons-2.0.9.RELEASE.jar:2.0.9.RELEASE]
 at org.springframework.data.mapping.PropertyPath.create(PropertyPath.java:392) ~[spring-data-commons-2.0.9.RELEASE.jar:2.0.9.RELEASE]



How-To
The method name should be updated as bellows:
    List<CommonConfig> findAllByOrderByConfigTypeAscKeyAsc();




Reference
[1] https://stackoverflow.com/questions/25380984/how-to-sort-by-multiple-properties-in-spring-data-jpa-derived-queries

No comments: