更改Spring Data findAll()方法的默认排序顺序 [英] Change default sort order for Spring Data findAll() method

查看:372
本文介绍了更改Spring Data findAll()方法的默认排序顺序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Spring Data JPA,我想知道是否可以更改Spring Data findAll()方法使用的实体的默认排序顺序吗?

I'm using Spring Data JPA and I wonder if it is possible to change the default sort order for a entity being used by the Spring Data findAll() method?

推荐答案

您应该可以通过以下任一方式来做到这一点:

You should be able to do this by either:

在spring-data 1.5及更高版本中,重写Interface中的findAll()方法,添加@Query注释并在Entity类中创建一个命名的Query,例如,如下所示:

in spring-data 1.5+, overriding the findAll() method in your Interface, adding the @Query annotation and creating a named Query in your Entity class like, for example, below:

实体

@Entity
@NamedQuery(name = "User.findAll", query="select u from User u order by u.address.town")
public class User{

}

存储库

public interface UserRepository extends ... <User, Long> {

    @Override
    @Query
    public Iterable<User> findAll();
}

通过创建自定义存储库实现:

by creating a custom repository implementation:

http://docs .spring.io/spring-data/jpa/docs/current/reference/html/#repositories.custom-implementations

这篇关于更改Spring Data findAll()方法的默认排序顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆