Spring Data JPA对嵌套集合进行排序 [英] Spring Data JPA sorting on nested collection

查看:387
本文介绍了Spring Data JPA对嵌套集合进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下域对象:

@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;
}

我想对名为"PropertyDefinition.value"的项目(例如标题")进行排序

I would like to sort the items for example "title" named PropertyDefinition.value

如何使用Spring Data JPA做到这一点?

How could I do that with Spring Data JPA?

Iterable<Item> items = itemRepository.findAll(new Sort("???"));

示例代码可在此处找到:
https://github.com/altfatterz/sort-poc/

example code can be found here:
https://github.com/altfatterz/sort-poc/

任何反馈表示赞赏.

推荐答案

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

You can use the JPA or Hibernate @OrderBy annotation:

@OneToMany
@OrderBy("value ASC") // sort by value ASC
private List<PropertyDefinition> propertyDefinitions;

否则,您可以创建自己的查询,以使用Criteria或HQL Query对它们进行排序.

Otherwise, you can create your own query to sort them with Criteria or HQL Query.

这篇关于Spring Data JPA对嵌套集合进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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