Reputation: 113
I am learning about implementing MVVM architecture in android and I have trouble understanding the process flow. I cannot understand how the dao
class is connected to the database class and how the POJO
from the model class is added to the database.
Here is what I have I understood.
Model class
:
It is a POJO class which holds the folder structure.
Dao
It holds all the CRUD operations that needs to be performed on the db with a method attached to them through which they can be called.
Database class
It extends RoomDatabase and creates and returns a db instance. It also holds an abstract method which returns the Dao.
Here is my code (I actually have more, but I have provided only the area I lack understanding):
` @Entity
public class InventoryModel {
@PrimaryKey (autoGenerate = true)
public int id;
private String itemName;
private int itemQuantity;
private double itemPrice;
public InventoryModel(String itemName, int itemQuantity, double itemPrice){
this.itemName = itemName;
this.itemQuantity = itemQuantity;
this.itemPrice = itemPrice;
}
2.Dao
@Dao
public interface InventoryModelDao {
@Query("SELECT * FROM InventoryModel")
LiveData<List<InventoryModel>> getAllInventoryItems();
@Insert(onConflict = OnConflictStrategy.ABORT)
void addItem(InventoryModel inventoryModel);
@Delete
void deleteItem(InventoryModel inventoryModel);
}
3.Database class (I have only included the abstract method. the excluded part returns an instance of db)
public abstract InventoryModelDao inventoryModelDao();
How exactly does the Dao
connect to the database
?
Upvotes: 0
Views: 75
Reputation: 745
To extend a_local_nobody's answer. You can find the generated classes under app\build\generated\source\apt\debug\PACKAGE\dao\InventoryModelDao_Impl
Upvotes: 1
Reputation: 8191
The Data access object generates a lot of boilerplate code for us underneath when we specify things such as @Delete
, @Insert
and @Query
operations and you won't ever see this generated code, but internally these methods are converted to methods which execute on the database itself.
The Dao
is therefore the operations which execute on your database through generated code internally
Upvotes: 2