Entity


@Target(allowedTargets = [AnnotationTarget.CLASS])
@Retention(value = AnnotationRetention.BINARY)
public 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 PrimaryKey. You can also use primaryKeys attribute to define the primary key.

Each entity must either have a no-arg constructor or a constructor whose parameters match fields (based on type and name). Constructor does not have to receive all fields 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 Ignore.

When a class is marked as an Entity, all of its fields are persisted. If you would like to exclude some of its fields, you can mark them with Ignore.

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

Example:

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

Summary

Public constructors

Entity(
    @NonNull String tableName,
    @NonNull Index[] indices,
    boolean inheritSuperIndices,
    @NonNull String[] primaryKeys,
    @NonNull ForeignKey[] foreignKeys,
    @NonNull String[] ignoredColumns
)

Public methods

final @NonNull ForeignKey[]

List of ForeignKey constraints on this entity.

final @NonNull String[]

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

final @NonNull Index[]

List of indices on the table.

final boolean

If set to true, any Index defined in parent classes of this class will be carried over to the current Entity.

final @NonNull String[]

The list of Primary Key column names.

final @NonNull String

The table name in the SQLite database.

Public constructors

Entity

public Entity(
    @NonNull String tableName,
    @NonNull Index[] indices,
    boolean inheritSuperIndices,
    @NonNull String[] primaryKeys,
    @NonNull ForeignKey[] foreignKeys,
    @NonNull String[] ignoredColumns
)

Public methods

getForeignKeys

public final @NonNull ForeignKey[] getForeignKeys()

List of ForeignKey constraints on this entity.

Returns
@NonNull ForeignKey[]

The list of 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 Ignore, but this is useful for ignoring fields inherited from parents.

Columns that are part of an Embedded field can not be individually ignored. To ignore columns from an inherited Embedded field, use the name of the field.

Returns
@NonNull String[]

The list of field names.

getIndices

public final @NonNull Index[] getIndices()

List of indices on the table.

Returns
@NonNull Index[]

The list of indices on the table.

getInheritSuperIndices

public final boolean getInheritSuperIndices()

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 Index 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 RoomWarnings.INDEX_FROM_PARENT_FIELD_IS_DROPPED or 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 PrimaryKey annotation on the field with PrimaryKey.autoGenerate set to true.

Returns
@NonNull String[]

The primary key of this Entity. Can be empty if the class has a field annotated with PrimaryKey.

getTableName

public final @NonNull String getTableName()

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

Returns
@NonNull String

The SQLite tableName of the Entity.