Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible in Room to ignore a field on a basic update

I have the following entity:

@Entity
class Foo(
    @PrimaryKey
    @ColumnInfo(name = "id")
    val id: Long,

    @ColumnInfo(name = "thing1")
    val thing1: String,

    @ColumnInfo(name = "thing2")
    val thing2: String,

    @ColumnInfo(name = "thing3")
    val thing3: String,

    @ColumnInfo(name = "thing4")
    val thing4: String
) {

    @ColumnInfo(name = "local")
    var local: String? = null

}

Where local is information that is not stored on the server, only local to the phone.

Currently when I pull information from the server GSON auto fills in my values, but since "local" does not come from the server it is not populate in that object.

Is there a way that when I call update I can have Room skip the update for the "local" column without writing a custom update to insert into all other columns except "local"? The pain point is that I could have many columns and each new column I add, I would have to add that to the custom insert statement.

I have also thought of a one-to-one mapping from the server entity to a new "local" entity, however now I have to deal with the pain of a join statement everywhere I get my entity since I need the local information.

I was hoping that I could do something like this:

@Entity
class Foo(
    @PrimaryKey
    @ColumnInfo(name = "id")
    val id: Long,

    @ColumnInfo(name = "thing1")
    val instructions: String,

    @ColumnInfo(name = "thing2")
    val instructions: String,

    @ColumnInfo(name = "thing3")
    val instructions: String,

    @ColumnInfo(name = "thing4")
    val instructions: String
) {

    @Ignore
    var local: String? = null

}

Using the @Ignore annotation, to try and ignore the local string on a generic update. Then provide a custom update statement to just save the local info

@Query("UPDATE foo SET local = :newLocal WHERE foo.id = :id")
fun updateLocal(id: Long, newLocal: String)

However ROOM seems to be smart enough to check that I used @Ignore on the local property and it will not compile with that update statement.

Any ideas?

like image 711
lostintranslation Avatar asked Apr 23 '19 06:04

lostintranslation


People also ask

How do you ignore fields in a room database?

transient : It will ignore this field while saving into database, BUT it will also ignore this field while parsing data which comes from server. @Ignore : It will only ignore this field while inserting data into database, but this field will participate into json parsing.

What is entity in room?

A Room entity includes fields for each column in the corresponding table in the database, including one or more columns that comprise the primary key. The following code is an example of a simple entity that defines a User table with columns for ID, first name, and last name: Kotlin Java.


1 Answers

Partial Updates got added to Room in 2.2.0

In Dao you do the following:

// Here you specify the target entity
@Update(entity = Foo::class)
fun update(partialFoo: PartialFoo)

And along your entity Foo create a PartialFoo containing the primary key and the fields you want to update.

@Entity 
class PartialFoo {
    @ColumnInfo(name = "id")
    val id: Long,

    @ColumnInfo(name = "thing1")
    val instructions: String,
}

https://stackoverflow.com/a/59834309/1724097

like image 101
Rawa Avatar answered Oct 02 '22 22:10

Rawa