Note: This article is part of the advanced Room series which covers all the details about the Room persistence library. You can read all the articles here:
- Introduction to Room Persistent Library in Android
- Data Access Objects — DAO in Room [You are here]
- Entity Relationship in Room
- How does Room work internally?
- Room Database Migrations
- Using Room with LiveData and other third-party libraries
Room — Data Access Objects(DAO)
In previous articles, we have covered how we can use Room persistence library to create a relational database very easily. Some of the advantages of using Room are compile-time query verification, no boilerplate code and easy integration with RxJava, LiveData and Kotlin Coroutines. All these advantages in Room are achieved using Data Access Objects or DAOs.
In this article, we going to discuss Data Access Objects
or DAOs
in detail.
In Room, Data Access Objects
or DAOs
are used to access your application’s persisted data. They are a better and modular way to access your database as compared to query builders or direct queries.
A DAO can be either an interface or an abstract class. If it’s an abstract class, it can optionally have a constructor that takes a RoomDatabase
as its only parameter. Room creates each DAO implementation at compile time.
You can perform multiple operations using DAO like Insertion, Updation, Deletion and making raw queries. Also, you can easily integrate LiveData, RxJava Observables, Kotlin Coroutines in DAOs.
Insertion
When you create a DAO method and annotate it with @Insert
, Room generates an implementation that inserts all parameters into the database in a single transaction.
onConflict
annotation parameter signifies what to do if a conflict happens on insertion. It can take the following values:
- OnConflictStrategy.REPLACE : To replace the old data and continue the transaction.
- OnConflictStrategy.ROLLBACK : To rollback the transaction.
- OnConflictStrategy.ABORT : To abort the transaction. The transaction is rolled back.
- OnConflictStrategy.FAIL : To fail the transaction. The transaction is rolled back.
- OnConflictStrategy.NONE : To ignore the conflict.
Note: ROLLBACK and FAIL strategies are deprecated. Use ABORT instead.
Updation
When you create a DAO method and annotate it with @Update
, Room generates an implementation that modifies a set of entities, given as parameters, in the database. It uses a query that matches against the primary key of each entity.
Deletion
When you create a DAO method and annotate it with @Delete
, Room generates an implementation that removes a set of entities, given as parameters, from the database. It uses the primary keys to find the entities to delete.
Simple queries
@Query
is the main annotation used in DAO classes. It allows you to perform read/write operations on a database. Each @Query
method is verified at compile time, so if there is a problem with the query, a compilation error occurs instead of a runtime failure.
Room also verifies the return value of the query such that if the name of the field in the returned object doesn’t match the corresponding column names in the query response, Room alerts you in one of the following two ways:
- It gives a warning if only some field names match.
- It gives an error if no field names match.
Passing parameters into the query
Parameters passed to the DAO methods can be used into the query written in @Query annotation.
Returning subsets of columns
You can also return subsets of columns from a query in Room.
Direct cursor access
If your app’s logic requires direct access to the return rows, you can return a Cursor
object from your queries.
Querying multiple tables
Some of your queries might require access to multiple tables to calculate the result. Room allows you to write any query, so you can also join tables. Furthermore, if the response is an observable data type, such as Flowable
or LiveData
, Room watches all tables referenced in the query for invalidation.
Query return types
Room supports a variety of return types for query methods, including specialised return types for interoperability with specific frameworks or APIs.
You can return LiveData
, Observable
and Flow
from query methods. Also, you can make a DAO method suspend function. These are discussed in separate articles.
Thank You!!!