2

I am trying to sort results of a get query by using a PagingAndSortingRepository extended interface, it all works fine until I try to sort by a field which is of type LocalDateTime.

I checked the logs, when I use this field, the query just ignores sorting and there is no order by clause whatsoever.

You can find a demo on here on GitHub.

Code

Model:
@Entity
@Table(name = "simple_users")
@DynamicInsert
@DynamicUpdate
public class SimpleUser implements Serializable
{
    private static final long serialVersionUID = 1L;

    // Columns
    protected Long id;
    protected String username;
    protected LocalDateTime createdAt;
    //...

    // Constructors
    public SimpleUser() { /** Default copy constructor */ }
    //...

    // Getters
    @Id
    @Column(name = "id", updatable = false)
    @JsonProperty("id")
    public Long getId () { return this.id; }

    @Column(name = "username")
    @JsonProperty("username")
    public String getUsername () { return this.username; }

    @Column(name = "created_at")
    @JsonProperty("created_at")
    public LocalDateTime getCreatedAt () { return this.createdAt; }
    //...
}
Interface:
@RepositoryRestResource(collectionResourceRel = "simple-users", path = "simple-users")
public interface SimpleUserInterface
    extends PagingAndSortingRepository<SimpleUser, Long>
{}
URL:
http://localhost:8080/simple-users?page=0&size=10&sort=created_at,desc

Update 1:

I have changed the JsonProperty name of the field from created_at to jjaa (an arbitary name) and the problem was solved; however I changed it jj_aa (an arbitary name with an underscore in-between) and the problem has risen again. It seems that there is a problem with naming json properties with underscore and sorting.

I would very much appreciate if anyone can tell me why this happens and preferably how to solve that, because changing so many names to avoid underscores means a heavy refactoring on both back and front-end.

Tala
  • 909
  • 10
  • 29
  • 1
    Wish I'd knew why it is downvoted so I could provide more detail if needed. – Tala Mar 27 '21 at 09:45
  • 1
    You use hephen "-" in `@RepositoryRestResource` but use understore "_" in URL. Highly suggest remove any custom configuration and try URL `http://localhost:8080/simpleUsers?page=0&size=10&sort=createdAt,desc` – yejianfengblue Mar 27 '21 at 09:59
  • @yejianfengblue Thanks! that is actually a typo and I have fixed it. Nevertheless, it refuses to order the results by the `created_at` field; I get the result, but in the default order. However, it orders by the `username` and `id` fields just fine. – Tala Mar 27 '21 at 10:05
  • 1
    Remove any unrelated configuration such as `@DynamicInsert`. Use the [H2 memory database](https://www.baeldung.com/spring-boot-h2-database) and [add test data](https://github.com/spring-guides/tut-rest/blob/master/rest/src/main/java/payroll/LoadDatabase.java) on application startup, then upload the project to Github. – yejianfengblue Mar 27 '21 at 10:07
  • Thanks @yejianfengblue. I have added a link to the github repo of a working demo. – Tala Mar 27 '21 at 10:56
  • 1
    The problem is not sort by LocalDateTime but sort by name with `_`. The easieast solution is don't use `_` in json name. According to https://stackoverflow.com/a/40862101/10009424, Spring Data REST supports sorting by name with `_` in some old version but rollback due to conflict with other functions. – yejianfengblue Mar 31 '21 at 00:59
  • 1
    Commented on https://github.com/spring-projects/spring-data-rest/issues/1638 I think issue may have been around a while – Kevvvvyp Mar 31 '21 at 21:54

0 Answers0