How can I dynamically sort a collection property in JPA?

We have the following JPA class:

@Entity class Supplier { // ... id property etc. @OneToMany @OrderBy("someProperty") private List<Region> regions; } 

This works fine in the normal case. However, we have several multilingual data in which values ​​are stored in properties such as nameEn , nameDe , nameZh . The exact property to use depends on the registered user. For example, a German-speaking user should see the regions as if they were annotated using @OrderBy("nameDe") .

How can I achieve this?

I know that I could sort the collection in my code after loading it, but this makes it difficult to paginate the results.

+8
java jpa
source share
1 answer

You can sort them in java. Maybe in getter:

 List<Region> getRegions(){ sorted = new List<Regions>(regions); Collections.sort(sorted, new RegionComparator(getUserLanguage())); return sorted; } 
+4
source share

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


All Articles