Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Room: @Ignore vs Transient

Are those two interchangable in context of Room database entity, or, if not, what are the differences between them?

like image 350
Pavlus Avatar asked Dec 08 '17 16:12

Pavlus


People also ask

What are the two annotations that every entity object class will have in room?

We'll use two annotations – @Embedded and @Relation. As I mentioned before – @Embedded allows nested fields to be referenced directly in the SQL queries. @Relation describes relations between two columns. We have to specify the parent column name, entity column name and entity class.

Is Android room an ORM?

Is Android Room an ORM? Room isn't an ORM; instead, it is a whole library that allows us to create and manipulate SQLite databases more easily. By using annotations, we can define our databases, tables, and operations.

What is room DB Android?

What is a Room database? Room is a database layer on top of an SQLite database. Room takes care of mundane tasks that you used to handle with an SQLiteOpenHelper . Room uses the DAO to issue queries to its database. By default, to avoid poor UI performance, Room doesn't allow you to issue queries on the main thread.

What is embedded in Room Android?

Marks a field of an Entity or POJO to allow nested fields (i.e. fields of the annotated field's class) to be referenced directly in the SQL queries. If the container is an Entity , these sub fields will be columns in the Entity 's database table.


2 Answers

@Ignore is a Room-specific annotation, saying that Room should ignore that field or method.

transient is a Java construct, indicating that this field should not be serialized in standard Java serialization. Room happens to treat this similarly to @Ignore by default. Mostly, that is there for cases where you are inheriting from some class that happens to use transient and you do not control that class (e.g., it is from a library).

For your own code, if you are not using Java serialization, I recommend sticking with @Ignore for the fields. transient is not an available keyword for a method, so to tell Room to ignore certain constructors, you have no choice but to use @Ignore.

like image 130
CommonsWare Avatar answered Oct 08 '22 03:10

CommonsWare


Adding to CommonsWare's answer


transient is not good option for ignoring fields for Room as CommonsWare answered. It will create blocker when same modal is being used to parse data from server and store into database.

Let's assume you have a modal class MyModal.java as below

public static class MyModal {      @SerializedName(“intField”)     public int intField;     @SerializedName(“strField”)     public String strField;     @SerializedName(“booleanField”)     public boolean booleanField; } 

If you want to NOT SAVE booleanField into database, and if you modified that field as

  1. transient : It will ignore this field while saving into database, BUT it will also ignore this field while parsing data which comes from server.
  2. @Ignore : It will only ignore this field while inserting data into database, but this field will participate into json parsing.
like image 37
Pankaj Kumar Avatar answered Oct 08 '22 04:10

Pankaj Kumar