Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use `@Ignore` field in `@Query` result using Room

I have two simple location and houses tables. I'd like to be able to fetch a location with all it's relevant information, and also add the house count to that query. The houseCount field is marked @Ignored since I don't want this saved. Room however can't seem to use the field for returning the data from a query like it would for any other class specified as a return object type for a query.

My field:

@Ignore
@ColumnInfo(name = EXTRA_COLUMN_LOCATION_HOUSE_COUNT)
public int houseCount;

My location constructor:

public Location(long id, String name, LatLng location, long defaultRent, Date synced, int houseCount) {...}

My query:

SELECT locations.*, COUNT(DISTINCT houses.id) AS house_count FROM locations, houses WHERE locations.id = :id AND houses.location_id = :id

The warning:

Warning:(37, 14) The query returns some columns [house_count] which are not use by ug.karuhanga.logrealty.Models.Entities.Location. You can use @ColumnInfo annotation on the fields to specify the mapping.  You can suppress this warning by annotating the method with @SuppressWarnings(RoomWarnings.CURSOR_MISMATCH). Columns returned by the query: id, name, location, default_rent, synced, house_count. Fields in ug.karuhanga.logrealty.Models.Entities.Location: id, name, location, default_rent, synced.

and error:

Error:(33, 8) error: Entities and Pojos must have a usable public constructor. You can have an empty constructor or a constructor whose parameters match the fields (by name and type).
Tried the following constructors but they failed to match:
Location(long,java.lang.String,ug.karuhanga.logrealty.Utils.LocationUtils.LatLng,long,java.util.Date,int) : [id : id, name : name, location : location, defaultRent : defaultRent, synced : synced, houseCount : null]
like image 587
Karuhanga Avatar asked Mar 25 '18 07:03

Karuhanga


1 Answers

Refer to this for workaround with @Ignore in room db. Room database build errors for @Ignore annotation

like image 114
Nischal Avatar answered Nov 02 '22 07:11

Nischal