Reputation: 14581
Here is a sample POJO
public class Product{
private long id;
private String name;
private double price;
... constructor for all fields
... getters and setters
}
Now, in my productDAO if I have a query like this
@Query(select id, name from products)
LiveData<List<Product>> getProducts()
I get an error like:
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) If I go in my Products and set
@Nullable
private double price;
the error remains.
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()
because @Ignore
is set the price is returned as 0.0.
So how to solve this? Hopefully I don't need to make a POJO for each different response from room...
Upvotes: 13
Views: 14886
Reputation: 708
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 tells Room to ignore the field altogether, which is not what you want, based on your answer.
Upvotes: 5