gpt4 book ai didi

android-room - Android Room 的部分实体绑定(bind)

转载 作者:行者123 更新时间:2023-12-03 21:27:04 31 4
gpt4 key购买 nike

想确认是否可以将实体 bean 绑定(bind)到表的部分列?

例子:

表“A”有列 id, col1, col2, col3, col4, col5, ... , col10

但是我只需要 id、col1、col2,所以我创建了一个带有字段 id、col1、col2 的实体 bean,并且只对这些字段进行绑定(bind)?我试过这样做,但得到:

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

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

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

最佳答案

返回列的子集

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


https://developer.android.com/topic/libraries/architecture/room.html
您可以定义一个没有 @Entity 的模型。注释并在您的选择查询中使用它。
// No @Entity annotation here !!!
public class NameTuple {
@ColumnInfo(name="first_name")
public String firstName;

@ColumnInfo(name="last_name")
public String lastName;
}
不必编写另一个 DAO(因为新类不是实体) - 您可以使用新类作为任何现有 DAO 中的返回数据类型。
// inside any existing DAO
@Query("SELECT first_name, last_name FROM user")
public List<NameTuple> loadFullName();

关于android-room - Android Room 的部分实体绑定(bind),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44819385/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com