室错误:查询返回的列没有字段fieldname [英] room error: The columns returned by the query does not have the fields fieldname

查看:298
本文介绍了室错误:查询返回的列没有字段fieldname的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个示例POJO

Here is a sample POJO

public class Product{
  private long id;
  private String name;
  private double price;

 ... constructor for all fields
 ... getters and setters
}

现在,如果我有这样的查询,请在我的productDAO中

Now, in my productDAO if I have a query like this

@Query(select id, name from products)
LiveData<List<Product>> getProducts()

我收到如下错误:

查询返回的列中没有字段[price] ...产品,即使它们被标注为非null或原始. 查询返回的列:[id,name]

The columns returned by the query does not have the fields [price] in ... Product even though they are annotated as non-null or primitive. Columns returned by the query: [id,name]

a)如果我进入我的产品并设置

a) If I go in my Products and set

@Nullable
private double price;

错误仍然存​​在.

b)如果我进入我的产品并设置

b) If I go in my Products and set

@Ignore
private double price;

错误消失了,但是如果我有另一个查询实例

the error goes away but if I have another query for instance

 @Query(select p.id, p.name, p.price, t.someField from products p inner join table t)
    LiveData<List<Product>> getJoinQueryResponse()

因为设置了@Ignore,所以价格返回为0.0.

because @Ignore is set the price is returned as 0.0.

那么如何解决呢?希望我不需要对来自房间的每个不同响应都做一个POJO ...

So how to solve this? Hopefully I don't need to make a POJO for each different response from room...

推荐答案

原始类型默认情况下不为null.将价格设为 Double (双精度),这样就可以解决该问题,因为届时它可以为空.此外,您可以添加自定义吸气剂,以避免将price作为空对象.

Primitive types are by default not null. Make the price Double and this will solve the issue since it will be nullable then. Furthermore, you can add a custom getter to avoid having price as a null object.

public double getPrice(){
    if(this.price == null) return 0.0;
    return this.price;
}

@Ingore 告诉Room根据您的答案完全忽略该字段,这不是您想要的.

@Ingore tells Room to ignore the field altogether, which is not what you want, based on your answer.

这篇关于室错误:查询返回的列没有字段fieldname的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆