2

I am trying to implement a Room database using a one-to-many relation, following the Android Room training (https://developer.android.com/training/data-storage/room/relationships#one-to-many).

However, I can't find a way to add a condition on the second table.

For example, to fetch all UserWithPlaylists, for users above 25 and having playlists names starting with "play" this does not work because Room does not automatically group the results (and a GROUP BY clause prevents creating the List of Playlist):

@Transaction
@Query("SELECT User.* FROM User, Playlist WHERE User.age > 25 AND Playlist.playlistName LIKE play%")
public List<UserWithPlaylists> getUsersWithPlaylistsSpecific();

I already tried solutions such as embedding one entity in another and using @Embedded(prefix = "PETS_") as suggested here: https://stackoverflow.com/a/50698610 but without success.
I also want to avoid this method using a Java Map.

How can I achieve this?

geckoflume
  • 399
  • 7
  • 11
  • The `@Embedded` solution with a table join is probably your best bet. `@Relation` doesn't allow you to add your own conditions on the completely separate query Room runs to retrieve the playlists for `UserWithPlaylists`. – krage Apr 30 '20 at 19:05
  • Thank you for your reply, that's too bad. This is what I did finally, with a mix of a loop solution for removing unwanted entities. Thanks again :) – geckoflume May 06 '20 at 20:55

0 Answers0