Spring Processing JPA Data on a Nested Collection

I have the following domain objects:

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

I would like to sort the elements, for example, "title" with the name PropertyDefinition.value

How can I do this using Spring JPA data?

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

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

Any feedback is appreciated.

+4
source share
1 answer

You can use the JPA annotation or Hibernate @OrderBy :

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

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

+7
source

Source: https://habr.com/ru/post/1493474/


All Articles