room从生成的查询返回不正确的初始化对象

bpzcxfmw  于 2021-06-21  发布在  Mysql
关注(0)|答案(1)|浏览(359)

我有三个表,一个包含卡片,一个包含卡片组,第三个实现前两个表之间的多对多关系,另外每个关系条目都包含一个符号。
我的任务是从card表中获取三列,从relation表中获取符号,并将其保存在一个专门为处理这些输入而设计的数据对象中,即所有条目都匹配给定的deckid。或者使用(希望是正确的)sql语言:

@Query("SELECT  R.symbol, C.title, C.type, C.source " +
        "FROM card_table C JOIN cards_to_card_deck R ON C.id = R.card_id"+
        "WHERE R.card_deck_id = :cardDeckId")
LiveData<List<CardWithSymbol>> getCardsWithSymbolInCardDeckById(long cardDeckId);

但是room实现类生成:

@Override
  public LiveData<List<CardWithSymbol>> getCardsWithSymbolInCardDeckById(long 
cardDeckId) {
    final String _sql = "SELECT  R.symbol, C.title, C.typ, C.source FROM 
  cards_to_card_deck R INNER JOIN card_table C ON R.card_id = C.id WHERE 
  R.card_deck_id = ?";
    final RoomSQLiteQuery _statement = RoomSQLiteQuery.acquire(_sql, 1);
    int _argIndex = 1;
    _statement.bindLong(_argIndex, cardDeckId);

return new ComputableLiveData<List<CardWithSymbol>>() {
  private Observer _observer;

  @Override
  protected List<CardWithSymbol> compute() {
    if (_observer == null) {
      _observer = new Observer("cards_to_card_deck","card_table") {
        @Override
        public void onInvalidated(@NonNull Set<String> tables) {
          invalidate();
        }
      };
      __db.getInvalidationTracker().addWeakObserver(_observer);
    }
    final Cursor _cursor = __db.query(_statement);
    try {
      final int _cursorIndexOfSymbol = _cursor.getColumnIndexOrThrow("symbol");
      final List<CardWithSymbol> _result = new ArrayList<CardWithSymbol>(_cursor.getCount());
      while(_cursor.moveToNext()) {
        final CardWithSymbol _item;
        final int _tmpSymbol;
        _tmpSymbol = _cursor.getInt(_cursorIndexOfSymbol);
        _item = new CardWithSymbol(_tmpSymbol,null,null,null);
        _result.add(_item);
      }
      return _result;
    } finally {
      _cursor.close();
    }
  }

  @Override
  protected void finalize() {
    _statement.release();
  }
}.getLiveData();

}
哪里
_item=new cardwithsymbol(_tmpsSymbol,null,null,null);
应该返回完全初始化的对象。
cardwithsymbol类声明如下:

public class CardWithSymbol {
public int symbol;
public String cardName;
public String cardType;
public String cardSource;

public CardWithSymbol(int symbol, String cardName, String cardType, String cardSource){
    this.symbol = symbol;
    this.cardName = cardName;
    this.cardType = cardType;
    this.cardSource = cardSource;

}
查询返回的列的类型为:
int符号,字符串标题,字符串类型,字符串源
我已经进行了一些调试,其余的应用程序工作正常。我甚至可以从查询返回的对象中读取符号,但是如上所述,出于某种原因,room忽略了其他三个参数,并在查询实现中将它们默认为null。

mzmfm0qo

mzmfm0qo1#

因此,在反复尝试并再次阅读dao文档之后,我发现了我的错误:
当创建一个类来处理room中列的子集时,告诉room哪个变量通过 @ColumnInfo(name = "name of the column goes here") -注解。
因此,将我的cardwithsymbol类更改为以下内容为我解决了问题:

import android.arch.persistence.room.ColumnInfo;

public class CardWithSymbol {

@ColumnInfo(name  = "symbol")
public int symbol;

@ColumnInfo(name = "title")
public String cardName;

@ColumnInfo(name = "type")
public String cardType;

@ColumnInfo(name = "source")
public String cardSource;

public CardWithSymbol(int symbol, String cardName, String cardType, String cardSource){
    this.symbol = symbol;
    this.cardName = cardName;
    this.cardType = cardType;
    this.cardSource = cardSource;
}

}

相关问题