Room Entities

Entities

An entity is backed by a Table in the Database. Each field gets a column unless it is marked as @Ignore. Entites are stored in the entities array in the Database. Fields need to be public or have getter and setters to persist, ie JavaBeans.

Entities must:

  • be annonated @Entity
  • must have atleast one primary key
  • Room must have access to the field. Fields must be public or have have getters and setters.
  • Fields need to have some way to convert to values that can be stored in SQLite. Room provides built-in support for primitives and their boxed alternatives. You'll also learn about TypeConverters later. If you have something that will not be stored in the database, such as the Bitmap in the example, you can use the @Ignore annotation, which tells Room to ignore fields or methods.

Primary Key Each entity must define at least 1 primary key.

composite primary keys:

@Entity(primaryKeys = {"firstName", "lastName"})
class User {
    public String firstName;
    public String lastName;

    @Ignore
    Bitmap picture;
}

set the table name, by default the class name is name of the table.

@Entity(tableName = "users")
class User {
    ...
}

set the column name with @ColumnInfo to change the default

@Entity(tableName = "users")
class User {
    @PrimaryKey
    public int id;

    @ColumnInfo(name = "first_name")
    public String firstName;

    @ColumnInfo(name = "last_name")
    public String lastName;

    @Ignore
    Bitmap picture;
}

indices speed up certain quieries in a database.

@Entity(indices = {@Index("name"),
        @Index(value = {"last_name", "address"})})
class User {
    @PrimaryKey
    public int id;

    public String firstName;
    public String address;

    @ColumnInfo(name = "last_name")
    public String lastName;

    @Ignore
    Bitmap picture;
}

Unique

@Entity(indices = {@Index(value = {"first_name", "last_name"},
        unique = true)})
class User {
    @PrimaryKey
    public int id;

    @ColumnInfo(name = "first_name")
    public String firstName;

    @ColumnInfo(name = "last_name")
    public String lastName;

    @Ignore
    Bitmap picture;
}

Foreign Key constraints

@Entity(foreignKeys = @ForeignKey(entity = User.class,
                                  parentColumns = "id",
                                  childColumns = "user_id"))
class Book {
    @PrimaryKey
    public int bookId;

    public String title;

    @ColumnInfo(name = "user_id")
    public int userId;
}

Foreign Keys allow actions to be defined when the refrenced entity is updated.

Embedded Composite of fields from a sub object

class Address {
    public String street;
    public String state;
    public String city;

    @ColumnInfo(name = "post_code")
    public int postCode;
}

@Entity
class User {
    @PrimaryKey
    public int id;

    public String firstName;

    @Embedded
    public Address address;
}

Entity Relationships

Types of relationships:

  1. 1-to-1: relationship between two entities where the first is exclusively linked to the second. ex: (a book and its ISBN)
  2. 1-to-many: 1 entity linked to 1 or more entities of a diffrent kind. ex: (library may own many books)
  3. Many-to-many: 1 or more entities of 1 kind can be linked to 1 or more entities of a diffrent kind. ex: (Authors and Books)

Kotlin

@Entity(tableName = "users")
data class User(@PrimaryKey
                @ColumnInfo(name = "userid")
                val id: String = UUID.randomUUID().toString(),
                @ColumnInfo(name = "username")
                val userName: String)

results matching ""

    No results matching ""