# Entity

Summary:[Methods](https://developer.android.com/reference/android/arch/persistence/room/Entity#pubmethods)\|[Inherited Methods](https://developer.android.com/reference/android/arch/persistence/room/Entity#inhmethods)  

# Entity

| The`android.arch`Architecture Components packages are no longer maintained. They have been superseded by the corresponding[androidx.\*](https://developer.android.com/jetpack/androidx/migrate)packages. See[androidx.room.Entity](https://developer.android.com/reference/androidx/room/Entity)instead.

`
public


abstract
@interface
Entity
`  
`


implements

Annotation


`

|--------------------------------------|
| android.arch.persistence.room.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 field annotated with[PrimaryKey](https://developer.android.com/reference/android/arch/persistence/room/PrimaryKey). You can also use[primaryKeys()](https://developer.android.com/reference/android/arch/persistence/room/Entity#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](https://developer.android.com/reference/android/arch/persistence/room/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](https://developer.android.com/reference/android/arch/persistence/room/Ignore).

If a field is`transient`, it is automatically ignored**unless** it is annotated with[ColumnInfo](https://developer.android.com/reference/android/arch/persistence/room/ColumnInfo),[Embedded](https://developer.android.com/reference/android/arch/persistence/room/Embedded)or[Relation](https://developer.android.com/reference/android/arch/persistence/room/Relation).

Example:  

```
 @Entity
 public class User {
   @PrimaryKey
   private final int uid;
   private String name;
   @ColumnInfo(name = "last_name")
   private String lastName;

   public User(int uid) {
       this.uid = uid;
   }
   public String getLastName() {
       return lastName;
   }
   public void setLastName(String lastName) {
       this.lastName = lastName;
   }
 }
 
```

<br />

**See also:**

- [Dao](https://developer.android.com/reference/android/arch/persistence/room/Dao)
- [Database](https://developer.android.com/reference/android/arch/persistence/room/Database)
- [PrimaryKey](https://developer.android.com/reference/android/arch/persistence/room/PrimaryKey)
- [ColumnInfo](https://developer.android.com/reference/android/arch/persistence/room/ColumnInfo)
- [Index](https://developer.android.com/reference/android/arch/persistence/room)

## Summary

|                                                                                                                                                                   ### Public methods                                                                                                                                                                    ||
|-----------------------------------------------------------------------------------------------------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| ` `[ForeignKey[]](https://developer.android.com/reference/android/arch/persistence/room/ForeignKey) | ` `[foreignKeys](https://developer.android.com/reference/android/arch/persistence/room/Entity#foreignKeys())`() ` List of[ForeignKey](https://developer.android.com/reference/android/arch/persistence/room/ForeignKey)constraints on this entity. |
| ` `[Index[]](https://developer.android.com/reference/android/arch/persistence/room)                 | ` `[indices](https://developer.android.com/reference/android/arch/persistence/room/Entity#indices())`() ` List of indices on the table.                                                                                                            |
| ` boolean`                                                                                          | ` `[inheritSuperIndices](https://developer.android.com/reference/android/arch/persistence/room/Entity#inheritSuperIndices())`() ` If set to`true`, any Index defined in parent classes of this class will be carried over to the current`Entity`.  |
| ` String[]`                                                                                         | ` `[primaryKeys](https://developer.android.com/reference/android/arch/persistence/room/Entity#primaryKeys())`() ` The list of Primary Key column names.                                                                                            |
| ` String`                                                                                           | ` `[tableName](https://developer.android.com/reference/android/arch/persistence/room/Entity#tableName())`() ` The table name in the SQLite database.                                                                                               |

| ### Inherited methods |
|-----------------------|---|
| From interface` java.lang.annotation.Annotation ` |-----------------------------------------|-------------------------| | ` abstract Class<? extends Annotation>` | ` annotationType() `    | | ` abstract boolean`                     | ` equals(Object arg0) ` | | ` abstract int`                         | ` hashCode() `          | | ` abstract String`                      | ` toString() `          | ||

## Public methods

### foreignKeys

```
ForeignKey[] foreignKeys ()
```

List of[ForeignKey](https://developer.android.com/reference/android/arch/persistence/room/ForeignKey)constraints on this entity.

<br />

|                                                                                                                    Returns                                                                                                                    ||
|--------------------------------------------------------------------------------------------------|---------------------------------------------------------------------------------------------------------------------------------------------|
| [ForeignKey[]](https://developer.android.com/reference/android/arch/persistence/room/ForeignKey) | The list of[ForeignKey](https://developer.android.com/reference/android/arch/persistence/room/ForeignKey)constraints on this entity. <br /> |

### indices

```
Index[] indices ()
```

List of indices on the table.

<br />

|                                                          Returns                                                           ||
|----------------------------------------------------------------------------------|------------------------------------------|
| [Index[]](https://developer.android.com/reference/android/arch/persistence/room) | The list of indices on the table. <br /> |

### inheritSuperIndices

```
boolean inheritSuperIndices ()
```

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](https://developer.android.com/reference/android/arch/persistence/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[INDEX_FROM_PARENT_FIELD_IS_DROPPED](https://developer.android.com/reference/android/arch/persistence/room/RoomWarnings#INDEX_FROM_PARENT_FIELD_IS_DROPPED)or[INDEX_FROM_PARENT_IS_DROPPED](https://developer.android.com/reference/android/arch/persistence/room/RoomWarnings#INDEX_FROM_PARENT_IS_DROPPED)warning during compilation.

<br />

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

### primaryKeys

```
String[] primaryKeys ()
```

The list of Primary Key column names.

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

<br />

|                                                                                                Returns                                                                                                 ||
|------------|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| `String[]` | The primary key of this Entity. Can be empty if the class has a field annotated with[PrimaryKey](https://developer.android.com/reference/android/arch/persistence/room/PrimaryKey). <br /> |

### tableName

```
String tableName ()
```

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

<br />

|                       Returns                        ||
|----------|--------------------------------------------|
| `String` | The SQLite tableName of the Entity. <br /> |

-

  ## Annotations

  - [ColumnInfo](https://developer.android.com/reference/android/arch/persistence/room/ColumnInfo)
  - [ColumnInfo.Collate](https://developer.android.com/reference/android/arch/persistence/room/ColumnInfo.Collate)
  - [ColumnInfo.SQLiteTypeAffinity](https://developer.android.com/reference/android/arch/persistence/room/ColumnInfo.SQLiteTypeAffinity)
  - [Dao](https://developer.android.com/reference/android/arch/persistence/room/Dao)
  - [Database](https://developer.android.com/reference/android/arch/persistence/room/Database)
  - [Delete](https://developer.android.com/reference/android/arch/persistence/room/Delete)
  - [Embedded](https://developer.android.com/reference/android/arch/persistence/room/Embedded)
  - [Entity](https://developer.android.com/reference/android/arch/persistence/room/Entity)
  - [ForeignKey](https://developer.android.com/reference/android/arch/persistence/room/ForeignKey)
  - [ForeignKey.Action](https://developer.android.com/reference/android/arch/persistence/room/ForeignKey.Action)
  - [Ignore](https://developer.android.com/reference/android/arch/persistence/room/Ignore)
  - [Index](https://developer.android.com/reference/android/arch/persistence/room)
  - [Insert](https://developer.android.com/reference/android/arch/persistence/room/Insert)
  - [OnConflictStrategy](https://developer.android.com/reference/android/arch/persistence/room/OnConflictStrategy)
  - [PrimaryKey](https://developer.android.com/reference/android/arch/persistence/room/PrimaryKey)
  - [Query](https://developer.android.com/reference/android/arch/persistence/room/Query)
  - [RawQuery](https://developer.android.com/reference/android/arch/persistence/room/RawQuery)
  - [Relation](https://developer.android.com/reference/android/arch/persistence/room/Relation)
  - [SkipQueryVerification](https://developer.android.com/reference/android/arch/persistence/room/SkipQueryVerification)
  - [Transaction](https://developer.android.com/reference/android/arch/persistence/room/Transaction)
  - [TypeConverter](https://developer.android.com/reference/android/arch/persistence/room/TypeConverter)
  - [TypeConverters](https://developer.android.com/reference/android/arch/persistence/room/TypeConverters)
  - [Update](https://developer.android.com/reference/android/arch/persistence/room/Update)
-

  ## Classes

  - [DatabaseConfiguration](https://developer.android.com/reference/android/arch/persistence/room/DatabaseConfiguration)
  - [InvalidationTracker](https://developer.android.com/reference/android/arch/persistence/room/InvalidationTracker)
  - [InvalidationTracker.Observer](https://developer.android.com/reference/android/arch/persistence/room/InvalidationTracker.Observer)
  - [Room](https://developer.android.com/reference/android/arch/persistence/room/Room)
  - [RoomDatabase](https://developer.android.com/reference/android/arch/persistence/room/RoomDatabase)
  - [RoomDatabase.Builder](https://developer.android.com/reference/android/arch/persistence/room/RoomDatabase.Builder)
  - [RoomDatabase.Callback](https://developer.android.com/reference/android/arch/persistence/room/RoomDatabase.Callback)
  - [RoomDatabase.MigrationContainer](https://developer.android.com/reference/android/arch/persistence/room/RoomDatabase.MigrationContainer)
  - [RoomWarnings](https://developer.android.com/reference/android/arch/persistence/room/RoomWarnings)
  - [RxRoom](https://developer.android.com/reference/android/arch/persistence/room/RxRoom)
-

  ## Enums

  - [RoomDatabase.JournalMode](https://developer.android.com/reference/android/arch/persistence/room/RoomDatabase.JournalMode)
-

  ## Exceptions

  - [EmptyResultSetException](https://developer.android.com/reference/android/arch/persistence/room/EmptyResultSetException)