# Entity

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

*** ** * ** ***


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

<br />

*** ** * ** ***

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

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

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

When a class is marked as an `https://developer.android.com/reference/androidx/room3/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/androidx/room3/Ignore` or named the ignored columns in `https://developer.android.com/reference/androidx/room3/Entity#ignoredColumns()`.

The persisted properties types define the column type affinity of the mapping SQLite table. Supported types are primitives, `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-string/index.html`, `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-byte-array/index.html` and enums. Additional types can be supported via `https://developer.android.com/reference/androidx/room3/ColumnTypeConverter` functions.

Example:

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

If a property is `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.jvm/-transient/index.html`, it is automatically ignored **unless** it is annotated with `https://developer.android.com/reference/androidx/room3/ColumnInfo`, `https://developer.android.com/reference/androidx/room3/Embedded` or `https://developer.android.com/reference/androidx/room3/Relation`.

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

## Summary

| ### Public methods |
|---|---|
| `final @https://developer.android.com/reference/androidx/annotation/NonNull ForeignKey[]` | `https://developer.android.com/reference/androidx/room3/Entity#getForeignKeys()()` List of `https://developer.android.com/reference/androidx/room3/ForeignKey` constraints on this entity. |
| `final @https://developer.android.com/reference/androidx/annotation/NonNull String[]` | `https://developer.android.com/reference/androidx/room3/Entity#getIgnoredColumns()()` The list of column names that should be ignored by Room. |
| `final @https://developer.android.com/reference/androidx/annotation/NonNull Index[]` | `https://developer.android.com/reference/androidx/room3/Entity#getIndices()()` List of indices on the table. |
| `final boolean` | `https://developer.android.com/reference/androidx/room3/Entity#getInheritSuperIndices()()` If set to `true`, any `https://developer.android.com/reference/androidx/room3` defined in parent classes will be carried over to this entity. |
| `final @https://developer.android.com/reference/androidx/annotation/NonNull String[]` | `https://developer.android.com/reference/androidx/room3/Entity#getPrimaryKeys()()` The list of Primary Key column names. |
| `final @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/java/lang/String.html` | `https://developer.android.com/reference/androidx/room3/Entity#getTableName()()` The table name in the SQLite database. |
| `final boolean` | `https://developer.android.com/reference/androidx/room3/Entity#getWithoutRowId()()` If set to `true`, the mapping SQLite table will be created using the `WITHOUT ROWID` option. |

## Public methods

### getForeignKeys

```
public final @NonNull ForeignKey[] getForeignKeys()
```

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

| Returns |
|---|---|
| `@https://developer.android.com/reference/androidx/annotation/NonNull ForeignKey[]` | The list of `https://developer.android.com/reference/androidx/room3/ForeignKey` constraints on this entity. |

### getIgnoredColumns

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

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

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

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

| Returns |
|---|---|
| `@https://developer.android.com/reference/androidx/annotation/NonNull String[]` | The list of property names. |

### getIndices

```
public final @NonNull Index[] getIndices()
```

List of indices on the table.

| Returns |
|---|---|
| `@https://developer.android.com/reference/androidx/annotation/NonNull Index[]` | The list of indices on the table. |

### getInheritSuperIndices

```
public final boolean getInheritSuperIndices()
```

If set to `true`, any `https://developer.android.com/reference/androidx/room3` defined in parent classes will be carried over to this 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/androidx/room3` 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/androidx/room3/RoomWarnings#INDEX_FROM_PARENT_FIELD_IS_DROPPED()` or `https://developer.android.com/reference/androidx/room3/RoomWarnings#INDEX_FROM_PARENT_IS_DROPPED()` warning during compilation.

| Returns |
|---|---|
| `boolean` | True if indices from parent classes should be automatically inherited by this Entity, false otherwise. Defaults to false. |

### getPrimaryKeys

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

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/androidx/room3/PrimaryKey` annotation on the property with `https://developer.android.com/reference/androidx/room3/PrimaryKey#autoGenerate()` set to `true`.

| Returns |
|---|---|
| `@https://developer.android.com/reference/androidx/annotation/NonNull String[]` | The primary key of this Entity. Can be empty if the class has a property annotated with `https://developer.android.com/reference/androidx/room3/PrimaryKey`. |

### getTableName

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

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

| Returns |
|---|---|
| `@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/java/lang/String.html` | The SQLite tableName of the Entity. |

### getWithoutRowId

```
public final boolean getWithoutRowId()
```

If set to `true`, the mapping SQLite table will be created using the `WITHOUT ROWID` option.

This is useful for tables that have small non-integer or composite (multi-column) primary keys as it can save space and improve performance by avoiding the generation of a separate row ID.

Note that tables without row ID cannot set `https://developer.android.com/reference/androidx/room3/PrimaryKey#autoGenerate()` to `true` on their primary key property.

See also [WITHOUT ROWID Optimization Documentation](https://sqlite.org/withoutrowid.html)

| Returns |
|---|---|
| `boolean` | True if the table should be created without row ID, false otherwise. Defaults to false. |