Thursday, 18 October 2018

Query type-converted list of objects in Room

I have a table that looks like following

@Entity
@JsonIgnoreProperties(ignoreUnknown = true)
public class Product
{
    @PrimaryKey
    @ColumnInfo(name = "ID")
    @JsonProperty("ID")
    public int id;

    @ColumnInfo(name = "Name")
    @JsonProperty("Name")
    public String name;

    @ColumnInfo(name = "Documents")
    @JsonProperty("Documents")
    @TypeConverters(DocumentConverter.class)
    public List<Document> documents;
}

//...

@TypeConverters(DocumentConverter.class)
@JsonIgnoreProperties( ignoreUnknown = true )
@JsonTypeName("Documents")
public class Document
{
    @JsonProperty("Name")
    public String name;

    @JsonProperty("URL")
    public String url;
}

I am able to retrieve a product based on its name by doing something like this

@Query("SELECT * FROM Product WHERE Name = :name")
List<Product> getProducts(String name);

And I would then be able to access the list of documents from each Product object. However I would also like to only deal with Products that has certain documents. I could get all Products via a query like above, then manually filter for the documents that I want, but it becomes quite a pain when I'm only looking for very specific documents.

Is it possible to also query based on Document variables without it being a separate table?

Something like...

@Query("SELECT * FROM Product WHERE Name = :name AND Document.name = :documentName")
List<Product> getProducts(String name, String documentName);

Thanks.



from Query type-converted list of objects in Room

No comments:

Post a Comment