# Embedded

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

*** ** * ** ***


```
@Target(allowedTargets = [AnnotationTarget.FIELD, AnnotationTarget.FUNCTION])
@Retention(value = AnnotationRetention.BINARY)
public annotation Embedded
```

<br />

*** ** * ** ***

Marks a field of an `https://developer.android.com/reference/androidx/room/Entity` or POJO to allow nested fields (i.e. fields of the annotated field's class) to be referenced directly in the SQL queries.

If the container is an `https://developer.android.com/reference/androidx/room/Entity`, these sub fields will be columns in the `https://developer.android.com/reference/androidx/room/Entity`'s database table.

For example, if you have 2 classes:

```
data class Coordinates (
  val latitude: Double,
  val longitude: Double
)

data class Address (
  val street: String,
  @Embedded
  val coordinates: Coordinates
)
```

Room will consider `latitude` and `longitude` as if they are fields of the `Address` class when mapping an SQLite row to `Address`.

So if you have a query that returns `street, latitude, longitude`, Room will properly construct an `Address` class.

If the `Address` class is annotated with `https://developer.android.com/reference/androidx/room/Entity`, its database table will have 3 columns: `street`, `latitude` and `longitude`.

If there is a name conflict with the fields of the sub object and the owner object, you can specify a `https://developer.android.com/reference/androidx/room/Embedded#prefix()` for the fields of the sub object. Note that prefix is always applied to sub fields even if they have a `https://developer.android.com/reference/androidx/room/ColumnInfo` with a specific `name`.

If sub fields of an embedded field has `https://developer.android.com/reference/androidx/room/PrimaryKey` annotation, they **will not** be considered as primary keys in the owner `https://developer.android.com/reference/androidx/room/Entity`.

When an embedded field is read, if all fields of the embedded field (and its sub fields) are `null` in the android.database.Cursor, it is set to `null`. Otherwise, it is constructed.

Note that even if you have `https://developer.android.com/reference/androidx/room/TypeConverter`s that convert a `null` column into a `non-null` value, if all columns of the embedded field in the android.database.Cursor are null, the `https://developer.android.com/reference/androidx/room/TypeConverter` will never be called and the embedded field will not be constructed.

You can override this behavior by annotating the embedded field with androidx.annotation.NonNull.

## Summary

| ### Public methods |
|---|---|
| `final @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/java/lang/String.html` | `https://developer.android.com/reference/androidx/room/Embedded#getPrefix()()` Specifies a prefix to prepend the column names of the fields in the embedded fields. |

## Public methods

### getPrefix

```
public final @NonNull String getPrefix()
```

Specifies a prefix to prepend the column names of the fields in the embedded fields.

For the example above, if we've written:

```
@Embedded(prefix = "loc_")
val coordinates: Coordinates
```

The column names for `latitude` and `longitude` will be `loc_latitude` and `loc_longitude` respectively.

By default, prefix is the empty string.

| Returns |
|---|---|
| `@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/java/lang/String.html` | The prefix to be used for the fields of the embedded item. |