Junction

@Target([]) class Junction
androidx.room.Junction

Declares a junction to be used for joining a relationship.

If a Relation should use an associative table (also know as junction table or join table) then you can use this annotation to reference such table. This is useful for fetching many-to-many relations.

@Entity(primaryKeys = {"pId", "sId"})
  public class PlaylistSongXRef {
      int pId;
      int sId;
  }
  public class PlaylistWithSongs {
      @Embedded
      Playlist playlist;
      @Relation(
              parentColumn = "playlistId",
              entity = Song.class,
              entityColumn = "songId",
              associateBy = @Junction(
                      value = PlaylistSongXRef.class,
                      parentColumn = "pId",
                      entityColumn = "sId)
      )
      List<String> songs;
  }
  @Dao
  public interface MusicDao {
      @Query("SELECT * FROM Playlist")
      List<PlaylistWithSongs> getAllPlaylistsWithSongs();
  }
  

In the above example the many-to-many relationship between Song and Playlist has an associative table defined by the entity PlaylistSongXRef.

Summary

Public constructors

<init>(value: KClass<*>, parentColumn: String, entityColumn: String)

Declares a junction to be used for joining a relationship.

Properties

String

The junction column that will be used to match against the Relation#entityColumn().

String

The junction column that will be used to match against the Relation#parentColumn().

KClass<*>

An entity or database view to be used as a junction table when fetching the relating entities.

Public constructors

<init>

Junction(value: KClass<*>, parentColumn: String, entityColumn: String)

Declares a junction to be used for joining a relationship.

If a Relation should use an associative table (also know as junction table or join table) then you can use this annotation to reference such table. This is useful for fetching many-to-many relations.

@Entity(primaryKeys = {"pId", "sId"})
  public class PlaylistSongXRef {
      int pId;
      int sId;
  }
  public class PlaylistWithSongs {
      @Embedded
      Playlist playlist;
      @Relation(
              parentColumn = "playlistId",
              entity = Song.class,
              entityColumn = "songId",
              associateBy = @Junction(
                      value = PlaylistSongXRef.class,
                      parentColumn = "pId",
                      entityColumn = "sId)
      )
      List<String> songs;
  }
  @Dao
  public interface MusicDao {
      @Query("SELECT * FROM Playlist")
      List<PlaylistWithSongs> getAllPlaylistsWithSongs();
  }
  

In the above example the many-to-many relationship between Song and Playlist has an associative table defined by the entity PlaylistSongXRef.

See Also

Properties

entityColumn

val entityColumn: String

The junction column that will be used to match against the Relation#entityColumn().

If not specified it defaults to Relation#entityColumn().

parentColumn

val parentColumn: String

The junction column that will be used to match against the Relation#parentColumn().

If not specified it defaults to Relation#parentColumn().

value

val value: KClass<*>

An entity or database view to be used as a junction table when fetching the relating entities.

Return
KClass<*>: The entity or database view to be used as a junction table.