Search code examples
javaandroidandroid-room

Room's DAOimpl not generating correctly


I am trying to use Room library to access a DB, but DAO implementation is not generating well. In my DAO interface i have:

 @Query("SELECT b.position,b.name,b.capacity,b.currentAmount, it.name FROM bottles AS b " +
            "INNER JOIN bottle_ingredient AS bi ON bi.bottlePos=b.position " +
            "INNER JOIN ingredient_types AS it ON it.id=bi.ingredientId" +
            " WHERE position = :position")
    Bottle getBottleWithIngredientFromPosition(int position);

And my bottle class is the following:

public class Bottle {
    public int position;
    public String name;
    public int capacity;
    public int currentAmount;
    public String ingredientName;

    //Constructor, getters and setters
}

But when i create a new Bottle object with the DAO it gets created with "ingredientName" as null. I checked the implementation Room generates and is the following:

@Override
  public Bottle getBottleWithIngredientFromPosition(final int position) {
    final String _sql = "SELECT b.position,b.name,b.capacity,b.currentAmount, it.name FROM bottles AS b INNER JOIN bottle_ingredient AS bi ON bi.bottlePos=b.position INNER JOIN ingredient_types AS it ON it.id=bi.ingredientId WHERE position = ?";
    final RoomSQLiteQuery _statement = RoomSQLiteQuery.acquire(_sql, 1);
    int _argIndex = 1;
    _statement.bindLong(_argIndex, position);
    __db.assertNotSuspendingTransaction();
    final Cursor _cursor = DBUtil.query(__db, _statement, false, null);
    try {
      final int _cursorIndexOfPosition = 0;
      final int _cursorIndexOfName = 1;
      final int _cursorIndexOfCapacity = 2;
      final int _cursorIndexOfCurrentAmount = 3;
      final Bottle _result;
      if(_cursor.moveToFirst()) {
        final int _tmpPosition;
        _tmpPosition = _cursor.getInt(_cursorIndexOfPosition);
        final String _tmpName;
        if (_cursor.isNull(_cursorIndexOfName)) {
          _tmpName = null;
        } else {
          _tmpName = _cursor.getString(_cursorIndexOfName);
        }
        final int _tmpCapacity;
        _tmpCapacity = _cursor.getInt(_cursorIndexOfCapacity);
        final int _tmpCurrentAmount;
        _tmpCurrentAmount = _cursor.getInt(_cursorIndexOfCurrentAmount);
        _result = new Bottle(_tmpPosition,_tmpName,_tmpCapacity,_tmpCurrentAmount,null);
      } else {
        _result = null;
      }
      return _result;
    } finally {
      _cursor.close();
      _statement.release();
    }
  }

As we can see it calls the constructor with last parameter as null and never tries to get it from the DB. I downloaded SQLite DB and run the query manually and it works as espected


Solution

  • Try SELECT b.position,b.name,b.capacity,b.currentAmount, it.name AS ingredientName FROM ....

    • where .... is the rest of the query. i.e. the important part is aliasing the name column to be ingredientName using AS.

    i.e. you are supplying the value that can/will be associated with the ingredientName member variable.

    This is assuming that you want the value of the name column from the ingredient_types table to be the ingredientName.