# Entity

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

*** ** * ** ***

Common/AllAndroid/JVMNative/C/iOSJavaScript
- Cmn

  ```
  @Target(allowedTargets = [AnnotationTarget.CLASS])
  @Retention(value = AnnotationRetention.BINARY)
  annotation Entity
  ```

*** ** * ** ***

Marks a class as an entity. This class will have a mapping SQLite table in the database.

Each entity must have at least 1 field annotated with `https://developer.android.com/reference/kotlin/androidx/room/PrimaryKey`. You can also use `https://developer.android.com/reference/kotlin/androidx/room/Entity#primaryKeys()` attribute to define the primary key.

Each entity must either have a no-arg constructor or a constructor whose parameters match properties (based on type and name). Constructor does not have to receive all properties as parameters but if a field is not passed into the constructor, it should either be public or have a public setter. If a matching constructor is available, Room will always use it. If you don't want it to use a constructor, you can annotate it with `https://developer.android.com/reference/kotlin/androidx/room/Ignore`.

When a class is marked as an `https://developer.android.com/reference/kotlin/androidx/room/Entity`, all of its properties are persisted. If you would like to exclude some of its properties, you can mark them with `https://developer.android.com/reference/kotlin/androidx/room/Ignore`.

If a property is `transient`, it is automatically ignored **unless** it is annotated with `ColumnInfo`, `Embedded` or `Relation`.

Example:

```kotlin
@Entity
data class Song (
    @PrimaryKey
    val id: Long,
    val name: String,
    @ColumnInfo(name = "release_year")
    val releaseYear: Int
)
```

| See also |
|---|---|
| `https://developer.android.com/reference/kotlin/androidx/room/Dao` |   |
| `https://developer.android.com/reference/kotlin/androidx/room/Database` |   |
| `https://developer.android.com/reference/kotlin/androidx/room/PrimaryKey` |   |
| `https://developer.android.com/reference/kotlin/androidx/room/ColumnInfo` |   |
| `https://developer.android.com/reference/kotlin/androidx/room` |   |

## Summary

| ### Public constructors |
|---|---|
| `https://developer.android.com/reference/kotlin/androidx/room/Entity#Entity(kotlin.String,kotlin.Array,kotlin.Boolean,kotlin.Array,kotlin.Array,kotlin.Array)( tableName: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-string/index.html, indices: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-array/index.html<https://developer.android.com/reference/kotlin/androidx/room>, inheritSuperIndices: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-boolean/index.html, primaryKeys: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-array/index.html<https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-string/index.html>, foreignKeys: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-array/index.html<https://developer.android.com/reference/kotlin/androidx/room/ForeignKey>, ignoredColumns: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-array/index.html<https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-string/index.html> )` | Cmn |

| ### Public properties |
|---|---|---|
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-array/index.html<https://developer.android.com/reference/kotlin/androidx/room/ForeignKey>` | `https://developer.android.com/reference/kotlin/androidx/room/Entity#foreignKeys()` List of `https://developer.android.com/reference/kotlin/androidx/room/ForeignKey` constraints on this entity. | Cmn |
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-array/index.html<https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-string/index.html>` | `https://developer.android.com/reference/kotlin/androidx/room/Entity#ignoredColumns()` The list of column names that should be ignored by Room. | Cmn |
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-array/index.html<https://developer.android.com/reference/kotlin/androidx/room>` | `https://developer.android.com/reference/kotlin/androidx/room/Entity#indices()` List of indices on the table. | Cmn |
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-boolean/index.html` | `https://developer.android.com/reference/kotlin/androidx/room/Entity#inheritSuperIndices()` If set to `true`, any Index defined in parent classes of this class will be carried over to the current `Entity`. | Cmn |
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-array/index.html<https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-string/index.html>` | `https://developer.android.com/reference/kotlin/androidx/room/Entity#primaryKeys()` The list of Primary Key column names. | Cmn |
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-string/index.html` | `https://developer.android.com/reference/kotlin/androidx/room/Entity#tableName()` The table name in the SQLite database. | Cmn |

## Public constructors

### Entity

Cmn Added in [2.8.5](https://developer.android.com/jetpack/androidx/releases/room#2.8.5)

```
Entity(
    tableName: String = "",
    indices: Array<Index> = [],
    inheritSuperIndices: Boolean = false,
    primaryKeys: Array<String> = [],
    foreignKeys: Array<ForeignKey> = [],
    ignoredColumns: Array<String> = []
)
```

## Public properties

### foreignKeys

Cmn

```
val foreignKeys: Array<ForeignKey>
```

List of `https://developer.android.com/reference/kotlin/androidx/room/ForeignKey` constraints on this entity.

| Returns |
|---|---|
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-array/index.html<https://developer.android.com/reference/kotlin/androidx/room/ForeignKey>` | The list of `https://developer.android.com/reference/kotlin/androidx/room/ForeignKey` constraints on this entity. |

### ignoredColumns

Cmn

```
val ignoredColumns: Array<String>
```

The list of column names that should be ignored by Room.

Normally, you can use `https://developer.android.com/reference/kotlin/androidx/room/Ignore`, but this is useful for ignoring properties inherited from parents.

Columns that are part of an `https://developer.android.com/reference/kotlin/androidx/room/Embedded` property can not be individually ignored. To ignore columns from an inherited `https://developer.android.com/reference/kotlin/androidx/room/Embedded` property, use the name of the property.

| Returns |
|---|---|
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-array/index.html<https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-string/index.html>` | The list of property names. |

### indices

Cmn

```
val indices: Array<Index>
```

List of indices on the table.

| Returns |
|---|---|
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-array/index.html<https://developer.android.com/reference/kotlin/androidx/room>` | The list of indices on the table. |

### inheritSuperIndices

Cmn

```
val inheritSuperIndices: Boolean
```

If set to `true`, any Index defined in parent classes of this class will be carried over to the current `Entity`. Note that if you set this to `true`, even if the `Entity` has a parent which sets this value to `false`, the `Entity` will still inherit indices from it and its parents.

When the `Entity` inherits an index from the parent, it is **always** renamed with the default naming schema since SQLite **does not** allow using the same index name in multiple tables. See `https://developer.android.com/reference/kotlin/androidx/room` for the details of the default name.

By default, indices defined in parent classes are dropped to avoid unexpected indices. When this happens, you will receive a `https://developer.android.com/reference/kotlin/androidx/room/RoomWarnings#INDEX_FROM_PARENT_FIELD_IS_DROPPED()` or `https://developer.android.com/reference/kotlin/androidx/room/RoomWarnings#INDEX_FROM_PARENT_IS_DROPPED()` warning during compilation.

| Returns |
|---|---|
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-boolean/index.html` | True if indices from parent classes should be automatically inherited by this Entity, false otherwise. Defaults to false. |

### primaryKeys

Cmn

```
val primaryKeys: Array<String>
```

The list of Primary Key column names.

If you would like to define an auto generated primary key, you can use `https://developer.android.com/reference/kotlin/androidx/room/PrimaryKey` annotation on the property with `https://developer.android.com/reference/kotlin/androidx/room/PrimaryKey#autoGenerate()` set to `true`.

| Returns |
|---|---|
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-array/index.html<https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-string/index.html>` | The primary key of this Entity. Can be empty if the class has a property annotated with `https://developer.android.com/reference/kotlin/androidx/room/PrimaryKey`. |

### tableName

Cmn

```
val tableName: String
```

The table name in the SQLite database. If not set, defaults to the class name.

| Returns |
|---|---|
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-string/index.html` | The SQLite tableName of the Entity. |