使用房间持久性库更新参数 [英] Update with parameter using room persistent library

查看:89
本文介绍了使用房间持久性库更新参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何使用房间库更新整个行,@Update接受带@Entity注释的对象,并通过引用主键进行更新,但是如何通过其他参数(如update)更新某些值与单元格中的值匹配的更新一排.

//Simple update
@Update
int updateObject(ObjectEntity... objectEntities);

//Custom Update
@Query(UPDATE TABLENAME ????)
int updateObject(ObjetEntity objectEntity,String field);

我应该通过什么代替?这样,新的objectEntity会被字段值匹配的旧对象所代替.

解决方案

您必须提前知道要与之匹配的列,因为Room不允许您动态定义该列.假设您有一个实体Person,如下所示:

@Entity(tableName = "people")
public final class Person {

    @PrimaryKey
    @ColumnInfo(name = "uuid")
    public final String uuid;

    @ColumnInfo(name = "name")
    public final String name;

    @ColumnInfo(name = "is_alive")
    public final Boolean isAlive;

    public Person(String uuid, String name, Boolean isAlive) {
        this.uuid = uuid;
        this.name = name;
        this.isAlive = isAlive;
    }
}

并且您想根据name更新列is_alive.您可以将方法编写为:

@Query("UPDATE people SET is_alive= :alive WHERE name = :name")
public abstract int setIsAliveByName(String name, int alive);

如果您的实体有很多字段,那么这当然会很繁琐,因为您必须将每个字段作为一个单独的参数传递,并手动编写整个UPDATE查询.

另一种方法是先执行SELECT查询,以获取项目,使用新数据更新对象,然后将其保存回数据库.

在这一点上,您开始怀疑使用ORM是否真的使任何事情变得简单并且值得您花费时间...

How will I update an entire row using room library, the @Update take a @Entity annotated object and updates it via referencing primary key but how will I update via some other parameter like update where certain value matches a value in cell in a row.

//Simple update
@Update
int updateObject(ObjectEntity... objectEntities);

//Custom Update
@Query(UPDATE TABLENAME ????)
int updateObject(ObjetEntity objectEntity,String field);

What should I pass in place of ???? such that the new objectEntity is replaced by old one where the field value matches.

解决方案

You have to know which column you are matching against ahead of time as Room doesn't let you dynamically define the column. Let's say you've got an entity Person as follows:

@Entity(tableName = "people")
public final class Person {

    @PrimaryKey
    @ColumnInfo(name = "uuid")
    public final String uuid;

    @ColumnInfo(name = "name")
    public final String name;

    @ColumnInfo(name = "is_alive")
    public final Boolean isAlive;

    public Person(String uuid, String name, Boolean isAlive) {
        this.uuid = uuid;
        this.name = name;
        this.isAlive = isAlive;
    }
}

And you wanted to update the column is_alive depending on the name. You could write the method as:

@Query("UPDATE people SET is_alive= :alive WHERE name = :name")
public abstract int setIsAliveByName(String name, int alive);

This of course could get quite tedious if you have an entity which has many fields because you have to pass in each field as a separate parameter and write the entire UPDATE query by hand.

The alternative is to do a SELECT query to fetch the items first, update the objects with the new data and then save them back into the db.

It's at this point you start to wonder if using an ORM is actually making anything easier and is worth your time...

这篇关于使用房间持久性库更新参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆