DEV Community

Cover image for Room database. Entities
Tristan Elliott
Tristan Elliott

Posted on • Edited on

Room database. Entities

Introduction

  • This series is going to be dedicated to the basics of an SQLite database. I will be following the official google guide, HERE but I will be working in an order that makes more sense to me.

Youtube version

HERE

Getting started

  • Before we go any further I would highly recommend you read my article of what SQLite is, found HERE. That article will give you a solid understanding of SQL and SQLite.

  • You also need to make sure that you have the proper dependencies installed. The proper dependencies can be found HERE.

What is the Room database?

  • Room is a persistence library that provides an abstraction layer over SQLite to allow fluent database access while harnessing the full power of SQLite. It also gives us 3 main benefits:

1) : Compile time verification

2) : Convenient annotations that reduce boiler plate code

3) : Streamlined database migrations

  • Basically it is just an abstraction layer that makes working with SQLite a lot easier and gives us some added benefits.

Entities

  • So when we use the Room persistence library to store our app's data, we do so by defining entities to represent objects. Now the term entity is actually just a generic programming term that means a object in a system but in terms of a database, an entity is a object object holding information inside of SQLite. If you are unfamiliar with SQL terms, here is a quick review:

Entity : something inside of our database system.

Table : a set of rows, held in memory

Row : a set of columns that collectively describe an entity.

Column : an individual piece of data stored in a table

The Code

@Entity(tableName = "word_table")
public class Word {

    @PrimaryKey
    @NonNull
    @ColumnInfo(name = "word")
    private String mWord;

    public Word(@NonNull String word) {this.mWord = word;}

    public String getWord(){return this.mWord;}
}
Enter fullscreen mode Exit fullscreen mode
  • Now that we have to code defined, lets walkthrough it, line by line.

@Entity(tableName = "word_table")

  • @Entity is how we define a database entity in Room and (tableName = "word_table") is how we set the table name. (tableName = "word_table") is actually optional and if we don't define it the table's name will be the same as the class.

@PrimaryKey

  • Each Room entity must have a primary key. A primary key is used in relational database systems to uniquely identify a row inside of a table. @PrimaryKey is used assign the primary key and we must define a primary key when using Room.

@NonNull

  • Is a helpfully annotation that will make sure that at compile time our primary key is never null.

@ColumnInfo(name = "word")

  • This is how we define the column's name, it is optional and if not defined the column name will be the same as the variable's name.

public Word(@NonNull String word)

  • This is just a standard constructor that on instantiation, will create and assign a string to the primary key.

public String getWord()

  • This is a basic getter method, however, it is a very important getter method. In Room when creating an entity, we have to make sure every variable is public so that Room can create a column with it. If we define the variable as private like we did, we have to define a getter method, like we have. This getter method allows Room to access word and turn it into a primary key column.

Conclusion

  • Thank you for taking the time out of your day to read this blog post of mine. If you have any questions or concerns please comment below or reach out to me on Twitter.

Top comments (0)