Spring Data JPA对嵌套集合进行排序

Zol*_*ter 4 sorting spring hibernate jpa spring-data

我有以下域对象:

@Entity
public class Item {

   @Id
   @GeneratedValue(strategy = GenerationType.AUTO)
   private Long id;
   private String name;

   @OneToMany
   private List<PropertyDefinition> propertyDefinitions;
}

@Entity
public class PropertyDefinition {

   @Id
   private Long id;

   private final String name;
   private final String value;
}
Run Code Online (Sandbox Code Playgroud)

我想对项目进行排序,例如名为PropertyDefinition.value的"title"

我怎么能用Spring Data JPA做到这一点?

Iterable<Item> items = itemRepository.findAll(new Sort("???"));
Run Code Online (Sandbox Code Playgroud)

示例代码可以在这里找到:https:
//github.com/altfatterz/sort-poc/

任何反馈意见.

Sot*_*lis 8

您可以使用JPA或Hibernate @OrderBy注释:

@OneToMany
@OrderBy("value ASC") // sort by value ASC
private List<PropertyDefinition> propertyDefinitions;
Run Code Online (Sandbox Code Playgroud)

否则,您可以创建自己的查询以使用CriteriaHQL 对其进行排序Query.