无法使用会议室持久性获取查询结果 [英] Not able to get query results using Room persistence
问题描述
我正在使用Room Persistence库开发一个Android应用.我有一个用户和一个汽车实体
I am developing an Android app using the Room persistence library. I have a User and a Car entity
@Entity(tableName = "users")
public class User {
@PrimaryKey
@NonNull
private int id;
private String name;
public User(@NonNull int id, String name) {
this.id = id;
this.name = name;
}
}
和
@Entity(tableName = "cars", foreignKeys = @ForeignKey(parentColumns =
"id", childColumns = "userId", entity = User.class))
public class Car {
@PrimaryKey(autoGenerate = true)
private int id;
private int userId;
private String brand;
public Car(int userId, String brand) {
this.userId = userId;
this.brand = brand;
}
}
我还创建了一个UserWithCar类,如下所示:
Also I have created a UserWithCar class as below:
public class UserWithCar {
@Embedded(prefix = "user_")
public User user;
@Embedded(prefix = "car_")
public Car car;
}
正如您在UserWithCar中看到的那样,如果没有得到以下错误,我会使用前缀原因:
As you can see in the UserWithCar I use a prefix cause if I don't I get the following error:
多个字段具有相同的columnName:id.字段名称:用户> ID, 汽车> ID.
Multiple fields have the same columnName: id. Field names: user > id, car > id.
我想使用以下查询获取所有UserWithCar:
I want to get all the UserWithCar using the following query:
@Query("SELECT * FROM users JOIN cars ON users.id = cars.userId")
List<UserWithCar> getUserWithCar();
使用此查询,我收到以下错误:
Using this query I get the following error:
查询返回一些列[id,name,id,userId,brand] 不被com.roomdemo.data.models.UserWithCar使用.您可以使用 在字段上使用@ColumnInfo批注指定映射. com.roomdemo.data.models.UserWithCar有一些字段[user_id, user_name,car_id,car_userId,car_brand]不会返回 查询.如果不应该从结果中读取它们,则您 可以使用@Ignore批注对其进行标记.您可以禁止显示此警告 通过用 @SuppressWarnings(RoomWarnings.CURSOR_MISMATCH).传回的栏 查询:id,名称,id,userId,品牌.领域 com.foodtec.roomdemo.data.models.UserWithCar:user_id,user_name, car_id,car_userId,car_brand.
The query returns some columns [id, name, id, userId, brand] which are not use by com.roomdemo.data.models.UserWithCar. You can use @ColumnInfo annotation on the fields to specify the mapping. com.roomdemo.data.models.UserWithCar has some fields [user_id, user_name, car_id, car_userId, car_brand] which are not returned by the query. If they are not supposed to be read from the result, you can mark them with @Ignore annotation. You can suppress this warning by annotating the method with @SuppressWarnings(RoomWarnings.CURSOR_MISMATCH). Columns returned by the query: id, name, id, userId, brand. Fields in com.foodtec.roomdemo.data.models.UserWithCar: user_id, user_name, car_id, car_userId, car_brand.
我可以帮忙吗?谢谢!
更新 在@Wizard帮助下,我从@Embeded中删除了前缀,并为用户ID添加了@ColumnInfo"uId",为汽车ID添加了"cId",以便没有相同的ID字段.这样,它就可以工作了!>
Update Using @Wizard help, I removed the prefix from the @Embeded and I added @ColumnInfo "uId" for the User's id and "cId for the Car's id in order to not have the same id field. By this way it works!
推荐答案
查询返回的列:ID,名称,ID,用户ID,品牌.领域 com.foodtec.roomdemo.data.models.UserWithCar:user_id,user_name, car_id,car_userId,car_brand.
Columns returned by the query: id, name, id, userId, brand. Fields in com.foodtec.roomdemo.data.models.UserWithCar: user_id, user_name, car_id, car_userId, car_brand.
错误表明,查询返回的列与Pojo类不同.应该是一样的.或者,您可以使用@ColumnInfo
批注将Pojo变量映射到列名.
Error indicates that, columns returned by query is different from Pojo class. It should be the same. Alternatively you can map your Pojo variable to column name using @ColumnInfo
annotation.
例如,
@PrimaryKey
@NonNull
@ColumnInfo(name = "user_id")
private int id;
这样,id
将被映射到user_id
.
这篇关于无法使用会议室持久性获取查询结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!