ROOM is a Database which introduced in latest Android Architecture.
It acts as an abstraction layer for the existing SQLite APIs. All the required packages, parameters, methods, and variables are imported into an Android project by using simple annotations.
Step #1. Setup first with room gradle build into app level gradle file.
def room_version = "1.1.1" implementation "android.arch.persistence.room:runtime:$room_version" annotationProcessor "android.arch.persistence.room:compiler:$room_version"
Step #2. Create POJO.
@Entity (tabaleName = "CategoryTable")
//Room db creates table name with its class name by default if we want to change,
we can change by this property.
class Category{
//By default room uses variable name as column name .
private int @PrimaryKey id; //Atleast one field should be primary in room db.
//If you want to change column name also,
you need to use columnNameInfo annotation as below
@CollumnInfo(name = "category_name")
private String categoryName;
private String subCategoryId;
//So, here variables are private accessing them we have to called getter/setter
methodes.
}
Step #3. Each POJO we need to define a DAO . or a DataBase access Object. Its interface or an Abstract class to access db.
@Dao
public interface CategoryDao{
//Create, Read, Update and delete
//this dao represents each sqlite methods with annotation
@Insert(onConflicts=IGNORE)
void insertCategory(Category category);
@Query('Select * from Category')
public List findAllCategory();
@Update(onConflict = REPLACE)
void onUpdateCategory(Category category);
@Query('delete from category')
void deleteAllCategory();
}
Step #4. Create DataBase class it must be an Abstrasct class, So here I am creating MyCategoryDatabase, This class represents DataBase, so we have to add annotation called @DataBase . For this @DataBase annotation we must have to provide two properties first one is entities and secondly version, Entities means tables.
@DataBase (entities = {Category.class},version = 1)
public class abstract MyCategoryDataBase extends RoomDatabase{
//This class must contains abstract method that return an object of Data Access Object.
//So here I am creating this CategoryDao
public abstract CategoryDao getCategoryDaoObject();
}
So above are the three essential componant for working with the RoomDatabase.Step #5. Now, Save information into RoomDatabase. In MainActivity initialise RoomDatabase such as,
public class MainActivity extends AppCompactActivity
{
public static MyCategoryDataBase myCategoryDb;
//In onCreate() method init database
onCreate(){
myCategoryDb = Room. dataBaseBuilder(getApplicationContext(),
MyCategoryDatabase.class,"DataBaseName")./*Optional*/ allowMainThreadQueries().build();
//now create object of the entity class.
Category categorObj = new Category();
categorObj.setId(1);
categorObj.setCategoryName("Fruit Juice");
categorObj.setSubCategoryId("123");
//Point to remember RoomDatabase does not allow write db into main UI thread,
So please write data in seperate background thread.
//Even though if you want to allow it on main threrad use a method
called allowMainThreadQueries() before build().
myCategoryDb.getCategoryDaoObject().insertCategory(categorObj);
}
}
Important Annotations
Note: Always use a Thread, AsyncTask, or any worker threads to perform database operations.