mohammed talhi
mohammed talhi

Reputation: 1

ORDER BY DATETIME in Spring Boot API

I'm struggling to order my JSON list by datetime. Here's what I did

public class Post {
    @Id
    @GeneratedValue( strategy = GenerationType.AUTO )
    private long id;

    @Temporal(TemporalType.TIMESTAMP)
    @JsonFormat(pattern="yyyy-MM-dd HH:mm:ss")
    private Date postedAt;

And this is the query as it shown below from the repo :

@Query(value = "SELECT * FROM Post u  WHERE u.supervisor_id = ?1 
                ORDER BY convert(datetime,u.postedAt,121) ASC", nativeQuery = true)

List findPostsBySupervisorId(Long id);

but it's not working. What should I do to get my list ordered?

Upvotes: 0

Views: 169

Answers (1)

user1430636
user1430636

Reputation:

I am not big fan of @Query, there are better ways, e.g. repository methods like findAllByOrderByPostedAtAsc, but if you have to, then what is reason not using just order by u.postedAt ?

Upvotes: 1

Related Questions