如何使用Android Room实体仅更新某些列?

如何使用Android Room实体仅更新某些列?,android,android-room,android-architecture-components,Android,Android Room,Android Architecture Components,不幸的是,@Update注释只有冲突解决形式的有限功能。我的实体包含远程、不可变或本地的数据。因此,当从远程源更新实体时,我需要保留或忽略对某些列的更新 我想构建一个更新,从远程源获取一个对象,并且只更新某些列 我的实体如下所示: @Entity public class MyExample { @PrimaryKey public String remoteId; private String first; private String second;

不幸的是,@Update注释只有冲突解决形式的有限功能。我的实体包含远程、不可变或本地的数据。因此,当从远程源更新实体时,我需要保留或忽略对某些列的更新

我想构建一个更新,从远程源获取一个对象,并且只更新某些列

我的实体如下所示:

@Entity
public class MyExample {
    @PrimaryKey
    public String remoteId;
    private String first;
    private String second;
    // This property is local only, don't want it reset.
    private String third;
    // constructor, getter and setter
}
@Query("UPDATE my_example SET first = :first, second = :second, WHERE remoteId = :remoteId")
protected abstract int updateRemote(String remoteId, String first, String second);
@Query("UPDATE my_example SET first = :entity.first, second = :entity.second, WHERE remoteId = :entity.remoteId")
protected abstract int updateRemote(MyExample entity);
@Query("UPDATE my_example SET first = :entity.first, second = :entity.second, WHERE remoteId = :entity.remoteId")
protected abstract int updateRemote(MyExample entity);
这就是我现在正在做的:

@Entity
public class MyExample {
    @PrimaryKey
    public String remoteId;
    private String first;
    private String second;
    // This property is local only, don't want it reset.
    private String third;
    // constructor, getter and setter
}
@Query("UPDATE my_example SET first = :first, second = :second, WHERE remoteId = :remoteId")
protected abstract int updateRemote(String remoteId, String first, String second);
@Query("UPDATE my_example SET first = :entity.first, second = :entity.second, WHERE remoteId = :entity.remoteId")
protected abstract int updateRemote(MyExample entity);
@Query("UPDATE my_example SET first = :entity.first, second = :entity.second, WHERE remoteId = :entity.remoteId")
protected abstract int updateRemote(MyExample entity);
请注意,“第三个”未更新

然而,每次我想添加一个新的实体时,从头开始编写是非常繁琐和耗时的

我想做什么:

@Entity
public class MyExample {
    @PrimaryKey
    public String remoteId;
    private String first;
    private String second;
    // This property is local only, don't want it reset.
    private String third;
    // constructor, getter and setter
}
@Query("UPDATE my_example SET first = :first, second = :second, WHERE remoteId = :remoteId")
protected abstract int updateRemote(String remoteId, String first, String second);
@Query("UPDATE my_example SET first = :entity.first, second = :entity.second, WHERE remoteId = :entity.remoteId")
protected abstract int updateRemote(MyExample entity);
@Query("UPDATE my_example SET first = :entity.first, second = :entity.second, WHERE remoteId = :entity.remoteId")
protected abstract int updateRemote(MyExample entity);

@查询(“更新我的示例集第一个、第二个值(实体),其中remoteId=:entity.remoteId”) 受保护的抽象int updateRemote(MyExample实体)

任何这些都意味着我没有方法中每个参数的名称,我可以像传递普通@Update一样传递实体

为了更好地实现这一点,我们可以对仅在@Insert或@Update期间设置的列进行注释

有人知道这样做的方法吗,不用指定每个inout参数