Consider the following simple data class being used as a Room @Entity
@Entity
data class SimpleDataClass(
val name: String,
val timestamp: String,
val isInvalid: Boolean = false,
@PrimaryKey(autoGenerate = true) val id:Int=0
)
The functionality I am trying to implement is that two SimpleDataClass objects should be considered same if all there properties are same regardless of their id (which is only added to the class structure because it's going to be stored in a database). Eg. the timestamp only stores the dayOfMonth and the user did the same thing over many months
Note that as database records, they should be considered separate since the user really did generate two records hence their different auto incremented primary keys.
One can do this by writing explicit equals that ignores id comparison. But then one also needs to write other comparison functions like hashCode, toString etc. which I am trying to avoid (let the compiler generate them)
To let the compiler ignore id , I followed the advise here
@Entity
data class SimpleDataClass(
val name: String,
val timestamp: String,
val isInvalid: Boolean = false,
) {
@PrimaryKey(autoGenerate = true)
var id: Int=0
}
Note that id is now a var to allow for its setter
This achieves what I was looking for except that it breaks copying. When a record read from the database as a SimpleDataClass object is copied (via simpleDataClass.copy()), its id isn't transferred to the copy. To remedy that I am using
old.copy(/*change some params, or not*/).also { it.id = old.id}
Neither method feels satisfactory. How to achieve this?