Is there a way to control the order of child entity, when using one-to-many relationhips?

How can we define a custom query for child entity Playlist, so that we can have control over List playlists ordering?

I'm afraid there is no out-of-box way here.

Using @Relation annotation all you have is:

  • 5 annotation's parameters (associateBy, entity, entityColumn, parentColumn, projection. None of them has influence on order of child table's items)

  • Under the Relation's hood Room uses two queries - first you explicitly write in your DAO

SELECT * FROM User order by sort_key

and another - for fetching data from the child table (based on type of query's result and on @Relation parameters):

SELECT * FROM PlayList WHERE userCreatorId IN (<List of users id from the first query>)

This query is autogenerated and you @Relation annotation has no options to change item's order in it.

Of course, after getting this "unordered" result you can add some post-processing to achieve what you want manually


I'm not sure It is possible to add @Query to a field but what i'm sure is that you can use a particular collection to make the order. These are the steps:

  1. Make the Playlist entity emplements Comparable interface, then define the compareTo method following the sort_key attribute.

  2. Instead of list in UserWithPlaylists entity, use SortedSet or TreeSet this particular collection returns items ordered using natural order or Comparator.


You can implement this functionality in two ways :

1) You could use the Hibernate-specific @Sort annotation to perform in-memory (i.e. not in the database) sorting, using natural sorting or sorting using a Comparator you supply. For example, assume I have an MyComparator helper class that implements Comparator. Helper class can help you sort by sort_key. You could change Playlist collection like this:

@org.hibernate.annotations.Sort(type = SortType.COMPARATOR, comparator = MyComparator)
public Set<Playlist> playlists;

@Sort will perform sorting in-memory once the collection has been retrieved from the database.

2) You can use hibernate's @OrderBy annotation, which lets you specify a SQL fragment describing how to perform the sort.

@org.hibernate.annotations.OrderBy("sort_key")
 public Set<Playlist> playlists;

Hope that helps..!!