更改 Spring Data findAll() 方法的默认排序顺序 [英] Change default sort order for Spring Data findAll() method
本文介绍了更改 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+ 中,覆盖接口中的 findAll() 方法,添加 @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:
这篇关于更改 Spring Data findAll() 方法的默认排序顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文