Android Room:Query在继承的字段中返回null

时间:2017-09-28 07:10:13

标签: android android-room

我目前正在重构我的项目以使用Room Persistence Library(顺便说一句,喜欢它),但是我遇到了一个问题,我在这个问题中查询我的数据库是否存在继承自实体类。超类中的字段由于某种原因没有从查询中加载数据。

继承我的班级(顺便说一下,这是一个自定义的课程,而不是真正的课程,因为我无法分享真正的源代码):

Task.java:

public class Task extends SyncEntity {

    @PrimaryKey(autoGenerate = true)
    public final String taskId;

    public final String title;

     /** Status of the given task.
     * Enumerated Values: 0 (Active), 1 (Inactive), 2 (Completed)
     */
    @TypeConverters(StatusConverter.class)
    public final Status status;

    @TypeConverters(DateConverter.class)
    public Date startDate;

    public Examination(@NonNull String taskId,
                   @NonNull String title,
                   @NonNull Status status,
                   @NonNull Date startDate,
                   @NonNull Long createdAt,
                   @Nullable Long updatedAt,
                   @Nullable Boolean deleted) {
        super(createdAt, updatedAt, deleted);
        this.taskId = taskId;
        this.title = title;
        this.status = status;
    }

    @Ignore
    public Task(@NonNull Builder builder) {
        super(builder);
        this.taskId = builder.taskId;
        this.title = builder.title;
        this.status = builder.status;
    }

    ...
    ** left out for brevity **

}

和我的查询:

@Dao
public interface TaskRepository {

    @Query("SELECT " +
            "taskId, " +
            "title, " +
            "status, " +
            "startDate, " +
            "created_at AS createdAt, " +
            "updated_at AS updatedAt " +
            "FROM tasks " +
            "WHERE status = 0 " +
            "AND deleted = 0")
    LiveData<List<Task>> getActiveTasks();
    ...
    ** left out for brevity **
}

查询返回一个结果,其中createdAt为null,它不应该是,因为所有SyncEntities默认创建到System.currentTimeMillis()并且我已经双重检查该值是否存在于数据库中。 / p>

编译时我也收到以下警告,但我不明白为什么:

  

警告:(33,33)查询返回一些列[createdAt,updatedAt]   com.example.project.data.models.entities.Task不使用它们。   您可以在字段上使用@ColumnInfo注释来指定   映射。 com.example.project.data.models.entities.Task有一些   字段[created_at,updated_at,deleted],它们不会返回   查询。如果不应该从结果中读取它们,则可以   用@Ignore注释标记它们。您可以通过抑制此警告   用方法注释方法   @SuppressWarnings(RoomWarnings.CURSOR_MISMATCH)。返回的列   查询:taskId,title,status,startDate,createdAt,updatedAt。   com.example.project.data.models.entities.Task中的字段:   created_at,updated_at,deleted,taskId,title,status,startDate。

这是我的超级班: SynEntity.java

@Entity
public class SyncEntity implements Syncable {

    @NonNull
    @ColumnInfo(name = "created_at")
    public final Long createdAt;

    @Nullable
    @ColumnInfo(name = "updated_at")
    public final Long updatedAt;

    @Nullable
    public final Boolean deleted;

    SyncEntity(@NonNull Long createdAt,
               @Nullable Long updatedAt,
               @Nullable Boolean deleted) {
            this.createdAt = createdAt;
            this.updatedAt = updatedAt;
            this.deleted = deleted == null ? false : deleted;
    }

    @Ignore
    SyncEntity(@NonNull Builder<?> builder) {
            this.createdAt = builder.createdAt;
            this.updatedAt = builder.updatedAt;
            this.deleted = builder.deleted;
    }

    public static class Builder<T extends Builder<T>> {

        @NonNull
        private Long createdAt = System.currentTimeMillis();
        @Nullable
        private Long updatedAt = null;
        @NonNull
        private Boolean deleted = false;

        public Builder() {}

    ...
    ** left out for brevity **

}

这是房间或预期行为中的错误吗?

1 个答案:

答案 0 :(得分:1)

修改 正如@pskink在评论中指出的那样,问题就是查询。

问题在于查询:

@Override
public LiveData<List<UserPet>> loadUserAndPetNames() {
    final String _sql = "SELECT users.firstName AS firstName, 
    users.lastName AS lastName, pets.name AS petName, pets.created_at 
    AS createdAt, pets.updated_at AS updatedAt, pets.deleted AS deleted 
    FROM users, pets WHERE users.userId = pets.user_id";
    final RoomSQLiteQuery _statement = RoomSQLiteQuery.acquire(_sql, 0);

查询将列名返回为“createdAt”,但实体的字段使用“created_at”显式注释,因此它们不匹配。