JpaSort 与排序之间的区别

Difference between JpaSort vs Sort

我看到在使用 Spring Boot 时,有 JpaSort 和 Sort 可用于排序和分页。我试图找出这两者之间的区别,但找不到太多相关信息。

谁能从性能角度和实现角度告诉我这两者之间的区别?

(1) 什么是 JPA 元模型

Often, when we write a criteria query, we need to reference entity classes and their attributes. Now, one of the ways of doing this is to provide the attributes' names as strings. But, this has several downsides. The JPA Metamodel was introduced by the community to avoid these drawbacks and provide static access to the metadata of the managed entity classes. For one, we have to look up the names of entity attributes. And, in case a column name is changed later in the project lifecycle, we have to refactor each query where the name is being used.

来源https://www.baeldung.com/hibernate-criteria-queries-metamodel#prerequisites

(2) JpaSortSort

之间的继承
public class JpaSort extends Sort

(3) 权威定义.

Sort 查询的排序选项。您必须至少提供一个要排序的属性列表,该列表不得包含 null 或空字符串。方向默认为DEFAULT_DIRECTION.

JpaSort 包装 JPA 元模型属性 用于排序的查询的排序选项。

https://docs.spring.io/spring-data/data-jpa/docs/current/api/org/springframework/data/jpa/domain/JpaSort.html

让我们看例子

Page<Product> allProductsSortedByName = productRepository.findAll(Sort.by("name"));
Order order1 = new Order(Sort.Direction.DESC, "published");

List<Employee> list = repo.findByDepartment("Sales", JpaSort.unsafe("LENGTH(name)", "salary"));

(4)结论:JpaSort支持JPA元模型属性什么Sort不支持.