简体   繁体   English

Room DB 将 String @PrimaryKey 视为 @Embedded

[英]Room DB treating String @PrimaryKey as @Embedded

I am getting errors similar to below when I try to compile my project...当我尝试编译我的项目时,出现类似于以下的错误...

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

the super weird part is at the end of the error messages:超级奇怪的部分在错误消息的末尾:

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

it is trying to access properties inside the String class??它试图访问String class 中的属性??

my @Entity declaration is as follows我的@Entity声明如下

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

BooleanDao class...if i comment out the @Delete and delete , then error stops appearing BooleanDao class...如果我注释掉@Deletedelete ,则错误不再出现

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

inside build.gradle file里面build.gradle文件

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

i implemented the @Delete function wrong.我错误地实施了@Delete function。

according to @Delete JavaDoc, the parameters should either be an @Entity or a partial entity:根据@Delete JavaDoc,参数应该是@Entity或部分实体:

  • All of the parameters of the Delete method must either be classes annotated with {@link Entity} or collections/array of it. Delete 方法的所有参数必须是用 {@link Entity} 注释的类或它的集合/数组。

     @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.如果目标实体是通过 {@link #entity()} 指定的,那么参数可以是任意的 POJO 类型,这些类型将被解释为部分实体。 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); }

since the argument i passed in is not annotated with @Entity , then it was being interpreted as a partial entity....so it was looking for the fields from String inside my BooleanEntity class...因为我传入的参数没有用@Entity注释,所以它被解释为部分实体....所以它在我的BooleanEntity class 中寻找String中的字段...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM