I've recentyly started using Room and I would like to know if when inserting or updating an Entitiy there's any way to check if the value is null, and in that case, do not insert it /update it.
What I want to do is something like
@Entity
data class Person{
@PrimaryKey
val id: Int,
val name:String
val surname:String
}
Would it be possible in a simple way to perform an @Update operation for those fields which are not null? and those which are null keep them as tey are?
For example in an update perhaps I might have informed the id and the name, but in another update I might have informed the id and the surname. So what I want is to merge the information, but if possible without having to make a select query to check the values stored.
I've read the following post, but my doubt then it would be, is it possible to define an @Entity, with all the fields defined as the one I mentioned before and then have other entities to just update some fields, something like:
@Entity(tableName = "person")
data class PersonUpdateSurname{
@PrimaryKey
val id: Int,
val name:String
}
@Entity(tableName = "person")
data class PersonUpdateSurname{
@PrimaryKey
val id: Int,
val surname:String
}
Is there a way to tell Room which is the original table structure?
Is there a way to tell Room which is the original table structure?
This question is not clear. Maybe there is some misunderstanding you have.
Try to follow next schema:
Person
-related data class annotated with Room-annotations - @Entity, @PrimaryKey and so on. In your case it is Person
class.PersonName
and PersoneSurname
(with the fields you described but without Room's annotations).entity
-parameter in @Update:@Update(entity = Person::class)
fun updateName(personName: PersonName)
@Update(entity = Person::class)
fun updateSurname(personeSurname: PersonSurname)
In your Repository call method what you need. If you want to update only name - you use method updateName()
and instance of PersonName
class as a parameter, for only surname's update - method updateSurname()
and instance of class PersonSurname
.