android - 使用 Room 在 `@Ignore` 结果中使用 `@Query` 字段

标签 android android-room

我有两个简单的locationhouses 表。 我希望能够获取包含所有相关信息的 location,并将 house 计数添加到该查询中。 houseCount 字段被标记为 @Ignored 因为我不想保存它。然而,Room 似乎无法像使用指定为查询返回对象类型的任何其他类那样使用该字段从查询返回数据。

我的领域:

@Ignore
@ColumnInfo(name = EXTRA_COLUMN_LOCATION_HOUSE_COUNT)
public int houseCount;

我的位置构造函数:

public Location(long id, String name, LatLng location, long defaultRent, Date synced, int houseCount) {...}

我的查询:

SELECT locations.*, COUNT(DISTINCT houses.id) AS house_count FROM locations, houses WHERE locations.id = :id AND houses.location_id = :id

警告:

Warning:(37, 14) The query returns some columns [house_count] which are not use by ug.karuhanga.logrealty.Models.Entities.Location. You can use @ColumnInfo annotation on the fields to specify the mapping.  You can suppress this warning by annotating the method with @SuppressWarnings(RoomWarnings.CURSOR_MISMATCH). Columns returned by the query: id, name, location, default_rent, synced, house_count. Fields in ug.karuhanga.logrealty.Models.Entities.Location: id, name, location, default_rent, synced.

和错误:

Error:(33, 8) error: Entities and Pojos must have a usable public constructor. You can have an empty constructor or a constructor whose parameters match the fields (by name and type).
Tried the following constructors but they failed to match:
Location(long,java.lang.String,ug.karuhanga.logrealty.Utils.LocationUtils.LatLng,long,java.util.Date,int) : [id : id, name : name, location : location, defaultRent : defaultRent, synced : synced, houseCount : null]

最佳答案

请参阅此处以了解在房间数据库中使用@Ignore 的解决方法。 Room database build errors for @Ignore annotation

关于android - 使用 Room 在 `@Ignore` 结果中使用 `@Query` 字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49473662/

相关文章:

添加依赖项 com.google.android.material 后,Android Manifest Merger 失败

android - Toolbar fragment onoptionsitemselected not called kotlin,有几种方案推荐什么?

android - 在不同选项卡之间滑动很流畅,但选项卡的更改非常慢

android - 如何使用自定义 Artifact 名称将模块上传到 Bintray?

android - 房间内完全一对一的关系

android - 房间持久性库事务

android - Android Room 加入的返回类型

java - Android Retrofit 从服务器下载/读取文本文件

android - 房间: Cannot access database on the main thread *but*

android - 将 LiveData 与包含的不同对象合并