android 间具有复合键的房间中的实体中不存在主键

Primary key does not exist in the entity in android room with composite key

我得到的错误是:

error: theme_id, picture_id referenced in the primary key does not exists in the Entity. Available column names:theme_id, picture_id, image

我在网上广泛查找,但找不到任何相关信息。 这个错误似乎只在尝试使用复合键时弹出。 如果我用普通的 PrimaryKey 注释其中一个字段,它就可以正常工作。 我不明白这里发生了什么,令人沮丧的是我在网上找不到任何相关信息。 希望大家能帮帮我。

实体

@Entity(primaryKeys = {"theme_id, picture_id"}, tableName = "picture")
public class Picture {

    @ColumnInfo(name = "theme_id")
    private int themeId;
    @ColumnInfo(name = "picture_id")
    private int pictureId;
    @ColumnInfo(name = "image", typeAffinity = ColumnInfo.BLOB)
    private byte[] image;
}

@Dao
public interface PictureDao {

    @Insert
    void instertPictures(Picture... pictures);

    @Update
    void updatePictures(Picture... pictures);

    @Delete
    void deletePictures(Picture... pictures);

    @Query("SELECT * FROM picture")
    List<Picture> getAllPictures();

    @Query("SELECT * FROM picture WHERE theme_id = :themeId")
    List<Picture> getThemePictures(int themeId);

    @Query("SELECT * FROM picture WHERE theme_id = :themeId AND picture_id = :pictureId")
    Picture getPicture(int themeId, int pictureId);

}

数据库

@Database(entities = {Picture.class}, version = 1, exportSchema = false)
public abstract class PictureDatabase extends RoomDatabase {

    public static final String NAME = "picture_db";

    public abstract PictureDao pictureDao();
}

您收到此错误,因为您尚未创建名为 theme_id, picture_id 的列。可能您打算使用 2 个主键 theme_idpicture_id。然后你必须传递两个用逗号分隔的字符串,而不是一个里面有逗号的字符串。

所以改变

{"theme_id, picture_id"}
 "  only one string   "

{"theme_id", "picture_id"}
 " first  ", "  second  "
 " string "  "  string  "

它应该工作得很好。