Android 房间的部分实体绑定

Partial Entity Binding for Android Room

想确认是否可以将实体 bean 绑定到 table 的部分列?

示例:

Table "A" 有列 id,col1,col2,col3,col4,col5,...,col10

但我只需要 id、col1、col2,所以我创建了一个包含字段 id、col1、col2 的实体 bean 并只对这些字段进行绑定?我尝试这样做但得到了:

原因:java.lang.IllegalStateException:房间无法验证数据完整性。看起来您已更改架构但忘记更新版本号。您可以简单地通过增加版本号来解决这个问题。

感谢是否有人可以验证是否可以使用 Room Persistence Library 进行上述绑定。

(注意:为什么我的 table 中有一些列未在移动应用程序中使用。这些 tables 架构是服务器中某些 tables 的精确副本端,因此一些字段用于服务器端 webapp)

返回列的子集

Most of the time, you need to get only a few fields of an entity. For example, your UI might display just a user's first name and last name, rather than every detail about the user. By fetching only the columns that appear in your app's UI, you save valuable resources, and your query completes more quickly.

https://developer.android.com/topic/libraries/architecture/room.html

您可以定义一个没有 @Entity 注释的模型并在您的 select 查询中使用它。

// No @Entity annotation here !!!
public class NameTuple {
    @ColumnInfo(name="first_name")
    public String firstName;

    @ColumnInfo(name="last_name")
    public String lastName;
}

不必编写另一个 DAO(因为新的 class 不是一个实体)- 您可以使用新的 Class 作为任何现有 DAO 中的 return 数据类型。

// inside any existing DAO
    @Query("SELECT first_name, last_name FROM user")
    public List<NameTuple> loadFullName();