Room DB 将 String @PrimaryKey 视为 @Embedded

Room DB treating String @PrimaryKey as @Embedded

当我尝试编译我的项目时,出现类似于以下的错误...

error: Cannot find a column in the entity com.example.BooleanEntity that matches with this partial entity field. If you don't wish to use the field then you can annotate it with @Ignore. - coder in java.lang.String
error: Cannot find a column in the entity com.example.BooleanEntity that matches with this partial entity field. If you don't wish to use the field then you can annotate it with @Ignore. - hash in java.lang.String

超级奇怪的部分在错误消息的末尾:

....with @Ignore. - coder in java.lang.String
....with @Ignore. - hash in java.lang.String

它正在尝试访问 String class??

中的属性

我的@Entity声明如下

@Entity
data class BooleanEntity(
    @PrimaryKey val id:String,
    val value:Boolean,
)

BooleanDao class...如果我注释掉 @Deletedelete,则错误不再出现

@Dao
interface BooleanDao
{
    @Delete(entity = BooleanEntity::class)
    suspend fun delete(id:String)
}

build.gradle 文件中

dependencies {
    .....
    // room database
    implementation "androidx.room:room-ktx:2.4.2"
    kapt "androidx.room:room-compiler:2.4.2"
    .....
}

我实现了 @Delete 功能错误。

根据 @Delete JavaDoc,参数应该是 @Entity 或部分实体:

  • All of the parameters of the Delete method must either be classes annotated with {@link Entity} or collections/array of it.

    @Dao
    public interface MusicDao {
        @Delete
        public void deleteSongs(Song... songs);
    
        @Delete
        public void deleteAlbumAndSongs(Album album, List<Song> songs);
    }
    
  • If the target entity is specified via {@link #entity()} then the parameters can be of arbitrary POJO types that will be interpreted as partial entities. For example:

    @Entity
    public class Playlist {
      @PrimaryKey
      long playlistId;
      long ownerId;
      String name;
      @ColumnInfo(defaultValue = "normal")
      String category;
    }
    
    public class OwnerIdAndCategory {
      long ownerId;
      String category;
    }
    
    @Dao
    public interface PlaylistDao {
      @Delete(entity = Playlist.class)
      public void deleteByOwnerIdAndCategory(OwnerIdAndCategory... idCategory);
    }
    

因为我传入的参数没有用 @Entity 注释,所以它被解释为部分实体....所以它正在我的 String 中寻找字段=17=] class...