# Junction

Artifact: [androidx.room3:room3-common](https://developer.android.com/jetpack/androidx/releases/room3) [View Source](https://cs.android.com/search?q=file:androidx/room3/Junction.kt+class:androidx.room3.Junction) Added in [3.0.0](https://developer.android.com/jetpack/androidx/releases/room3#3.0.0)

*** ** * ** ***


```
@Target(allowedTargets = )
@Retention(value = AnnotationRetention.BINARY)
public annotation Junction
```

<br />

*** ** * ** ***

Declares a junction to be used for joining a relationship.

If a `https://developer.android.com/reference/androidx/room3/Relation` should use an associative table (also known 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"])
data class PlaylistSongXRef(
    val pId: Int,
    val sId: Int
)

data class PlaylistWithSongs(
    @Embedded
    val playlist: Playlist,
    @Relation(
        parentColumns = ["playlistId"],
        entity = Song::class,
        entityColumns = ["songId"],
        associateBy = Junction(
            value = PlaylistSongXRef::class,
            parentColumns = ["pId"],
            entityColumns = ["sId"]
        )
    )
    val songs: List<String>
)

@Dao
interface MusicDao {
    @Query("SELECT * FROM Playlist")
    suspend fun getAllPlaylistsWithSongs(): List<PlaylistWithSongs>
}
```

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

| See also |
|---|---|
| `https://developer.android.com/reference/androidx/room3/Relation` |   |

## Summary

| ### Public methods |
|---|---|
| `final @https://developer.android.com/reference/androidx/annotation/NonNull String[]` | `https://developer.android.com/reference/androidx/room3/Junction#getEntityColumns()()` The junction columns that will be used to match against the `https://developer.android.com/reference/androidx/room3/Relation#entityColumns()`. |
| `final @https://developer.android.com/reference/androidx/annotation/NonNull String[]` | `https://developer.android.com/reference/androidx/room3/Junction#getParentColumns()()` The junction columns that will be used to match against the `https://developer.android.com/reference/androidx/room3/Relation#parentColumns()`. |
| `final @https://developer.android.com/reference/androidx/annotation/NonNull https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.reflect/-k-class/index.html<@https://developer.android.com/reference/androidx/annotation/NonNull ?>` | `https://developer.android.com/reference/androidx/room3/Junction#getValue()()` An entity or database view to be used as a junction table when fetching the relating entities. |

## Public methods

### getEntityColumns

```
public final @NonNull String[] getEntityColumns()
```

The junction columns that will be used to match against the `https://developer.android.com/reference/androidx/room3/Relation#entityColumns()`.

If not specified it defaults to `https://developer.android.com/reference/androidx/room3/Relation#entityColumns()`.

### getParentColumns

```
public final @NonNull String[] getParentColumns()
```

The junction columns that will be used to match against the `https://developer.android.com/reference/androidx/room3/Relation#parentColumns()`.

If not specified it defaults to `https://developer.android.com/reference/androidx/room3/Relation#parentColumns()`.

### getValue

```
public final @NonNull KClass<@NonNull ?> getValue()
```

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

| Returns |
|---|---|
| `@https://developer.android.com/reference/androidx/annotation/NonNull https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.reflect/-k-class/index.html<@https://developer.android.com/reference/androidx/annotation/NonNull ?>` | The entity or database view to be used as a junction table. |