Search code examples
kotlinandroid-roomannotation-processingkapt

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??

my @Entity declaration is as follows

@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

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

inside build.gradle file

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

Solution

  • i implemented the @Delete function wrong.

    according to @Delete JavaDoc, the parameters should either be an @Entity or a partial 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);
      }
      

    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...