我有两个简单的location
和houses
表。
我希望能够获取location
及其所有相关信息,并将house
计数添加到该查询中。 houseCount
字段标记为@Ignored
,因为我不想保存此字段。然而,房间似乎无法使用该字段从查询中返回数据,就像为查询指定为返回对象类型的任何其他类一样。
我的领域:
@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]
答案 0 :(得分:0)
有关在Room db中使用@Ignore的解决方法,请参阅此。 Room database build errors for @Ignore annotation
答案 1 :(得分:0)
对于Kotlin,这对我有用:
1。像这样将@JvmOverloads
添加到您的数据类:
@Entity(tableName = "student")
data class Student @JvmOverloads constructor(
...
)
2。将@PrimaryKey
字段放置在构造函数的末尾,如下所示:
@Entity(tableName = "student")
data class Student @JvmOverloads constructor(
var name: String,
var age: Int,
var gpa: Double,
var isSingle: Boolean,
@Ignore
var isSelected: Boolean = false,
@PrimaryKey(autoGenerate = true) //must be placed at the end!!
var id: Long = 0
)