ORM libraries

An ORM (Object Relational Mapping) Library provides a better way for you to persist objects to a database instead, without worrying much about the SQL queries, and opening and closing database connections.

NOTE: You still need some level of SQL query knowledge 

There are a number of Android-compatible ORM Libraries:

  • ORMLite
  • GreenDAO
  • DbFlow
  • Room

But, in this book, we will focus on Room, which is an ORM introduced by Google.  

To use Room, we first have to add its dependencies to the project.

Open build.gradle, and add the following lines of code to the dependencies section:

implementation 'android.arch.persistence.room:runtime:1.0.0'
annotationProcessor 'android.arch.persistence.room:compiler:1.0.0'
kapt "android.arch.persistence.room:compiler:1.0.0"

Click on Sync Now. For Room to be able to save tasks to the database, we need to specify which class represents a table. This is done by annotating the class as an Entity. Open the Task class and replace its contents with the following lines of code:

@Entity(tableName = TodoListDBContract.TodoListItem.TABLE_NAME)
class Task() {

@PrimaryKey(autoGenerate = true)
@ColumnInfo(name = BaseColumns._ID)
var taskId: Long? = null

@ColumnInfo(name = TodoListDBContract.TodoListItem.COLUMN_NAME_TASK)
var taskDetails: String? = null

@ColumnInfo(name = TodoListDBContract.TodoListItem.COLUMN_NAME_DEADLINE)
var taskDeadline: String? = null

@ColumnInfo(name = TodoListDBContract.TodoListItem.COLUMN_NAME_COMPLETED)
var completed: Boolean? = false

@Ignore
constructor(taskDetails: String?, taskDeadline: String?): this() {
this.taskDetails = taskDetails
this.taskDeadline = taskDeadline
}

constructor(taskId:Long, taskDetails: String?, taskDeadline: String?, completed: Boolean) : this(taskDetails, taskDeadline) {
this.taskId = taskId
this.completed = completed
}

}

Here, the following applies:

  • @Entity specifies that Task represents a table in the database
  • @ColumnInfo maps the field to a database column
  • @PrimaryKey specifies that the field is the primary key of the table

The next thing is to create a DAO (Data Access Object). Create a new Kotlin interface with the name TaskDAO, and replace its contents with the following lines of code:

@Dao
interface TaskDAO {

@Query("SELECT * FROM " + TodoListDBContract.TodoListItem.TABLE_NAME)
fun retrieveTaskList(): List<Task>

@Insert
fun addNewTask(task: Task): Long

@Update
fun updateTask(task: Task)

@Delete
fun deleteTask(task: Task)

}

As shown in the preceding code, the following applies:

  • Room provides Insert, Update, and Delete annotations, so you don't have to write queries for those
  • For select operations, you have to annotate the method with the query

Next, we have to create a database class that will connect our application to the database. Create a new Kotlin class with the name AppDatabase, and replace its contents with the following code:

@Database(entities = arrayOf(Task::class), version = TodoListDBContract.DATABASE_VERSION)
abstract class AppDatabase : RoomDatabase() {
abstract fun taskDao(): TaskDAO
}

That's all the setup needed to connect to the database.

To use the database, open MainActivity. First, create a field of AppDatabase type:

private var database: AppDatabase? = null

Next, instantiate the field in the onCreate() method:

database = Room.databaseBuilder(applicationContext, AppDatabase::class.java, DATABASE_NAME).build()

Here, you specify your database class and the name of the database.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset