13

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...

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Alin
  • 14,809
  • 40
  • 129
  • 218
  • 1
    have you properly annotated your Product class? You could explicitly set the @ ColumnInfo annotation. Also, I don't see @ Entity either – Zun Jun 21 '18 at 11:43
  • https://developer.android.com/reference/android/arch/persistence/room/Entity – Zun Jun 21 '18 at 11:44
  • The Product Entity is defined in a separate file, the Pojo is used for Room query output handling. The Insert/Update will use the Entity format, but for the rest of the app I'll use the java POJO as it has many fields which are different from the entity. From my knowledge, the response fo @Query does not require an Entity class to output the result, just matching fields in it – Alin Jun 21 '18 at 11:45
  • So you have a Product Entity, and a POJO Product? Why are you writing duplicate code? Why does the entity have different fields from the pojo? – Zun Jun 21 '18 at 11:54
  • It looks duplicate but I have a few reasons for it. Imagine Products coming from a web service and having a different structure in json. To easy parse it, I still need a new POJO with similar structure. Then the web service structure changes, then change the POJO and not the entity. Not sure if it makes sense... I am still trying to figure out the best behavior. – Alin Jun 21 '18 at 13:02
  • @Alin can you post the Entity class too? It seems there is mismatch of fields somehow. – Stefano Mtangoo Jun 23 '18 at 08:05

1 Answers1

5

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.

Yogesh Umesh Vaity
  • 41,009
  • 21
  • 145
  • 105
Viktor Stojanov
  • 708
  • 6
  • 20
  • This feels like making thing harder when they should be simple. It seems that Double is `not recommended` to be used and I can't remember when I've used it. Another way to avoid this is adding in the query `0 as price`... again, making what should be plain simple more complicate. Thank you very much for your answer. – Alin Jun 21 '18 at 12:58
  • @Alin What do you mean with not recommended? The core difference by using primitive `double` instead of `Double` is the extra added `NOT NULL` annotation in your Room table, which is exactly the cause of your problem. – Viktor Stojanov Jun 21 '18 at 13:10
  • As seen at https://stackoverflow.com/questions/15582944/java-double-vs-double-class-type-vs-primitive-type Double has performance issues but probably this is not the case in my sample since getPrice will be a `double` – Alin Jun 21 '18 at 13:23
  • Its not working for me for custom Object primary key; even add type converter without returning null from type converter method; and rewrite getter method as you said but didn't work for me; – Javad Asoodeh May 21 '20 at 09:11