Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Query with object parameter

How to do a room query using a instance of a class as a data source: The following example will make my question clear:

@Query("UPDATE CATEGORIES SET Name = :item.Name, Active = :item.Active " +
"WHERE CategoryID = :item.CategoryID")
fun updateCategory(item: EntityCategory)

I tried

@Update
fun updateCategory(item: EntityCategory)

But that would delete all refrencing data

My Entity class:

@Entity(tableName = "CATEGORIES")
data class EntityCategory(
        @PrimaryKey(autoGenerate = false)
        var CategoryID: Long,
        var Name: String,
        var Active: Boolean) {
    constructor(item: DMCategory) : this(item.CategoryID, item.Name, item.Active)
}

I cant have my Primary key auto generated.

like image 665
Ivan Šimović Avatar asked Feb 15 '18 08:02

Ivan Šimović


People also ask

Can object pass as query parameter?

Yes, you can pass query parameters from some object.


1 Answers

Try

@Insert(onConflict = OnConflictStrategy.REPLACE)
like image 72
Caio henrique cavallari Avatar answered Oct 05 '22 14:10

Caio henrique cavallari