Room Persistence: Error:Entities and Pojos must have a usable public constructor

前端 未结 24 1755
长发绾君心
长发绾君心 2020-12-01 09:05

I\'m converting a project to Kotlin and I\'m trying to make my model (which is also my entity) a data class I intend to use Moshi to convert the JSON responses from the API<

相关标签:
24条回答
  • 2020-12-01 09:24

    For a variation on FutureShocked answer that implements autoGenerate:

    @Entity(tableName = "movies")
    data class MovieKt(
            var title: String,
            var overview: String,
            var poster_path: String,
            var backdrop_path: String,
            @Ignore var release_date: String,
            @Ignore var vote_average: Double,
            @Ignore var isFavorite: Int
    ) {
    @PrimaryKey(autoGenerate = true) var id : Int = 0
    
        constructor(title: String, overview: String, poster_path: String, backdrop_path: String) {
            this(id, title, overview, poster_path, backdrop_path, "", 0.0, 0)
        }
    }
    
    0 讨论(0)
  • 2020-12-01 09:26

    I had this problem with an entity (all fields were properly-initialized vars like a lot of the answers here are suggesting) that included a list of related, non-primitive items like the OP in this SO question had. For example:

    @Entity(tableName = "fruits")
    data class CachedFruitEntity(
            @PrimaryKey var id: Long = 0L,
            @Embedded(prefix = "buyer_") var buyerEntity: CachedBuyerEntity? = null
            @TypeConverters(VendorsConverter::class)
            var vendorEntities: List<CachedVendorEntity?> = listOf()))
    

    That is, it has an embedded field, and it took me a while to realize that what I actually needed was a type converter for the vendor entity list instead (the compiler wasn't throwing the usual Error:(58, 31) error: Cannot figure out how to save this field into database. You can consider adding a type converter for it. So my solution was very similar to this answer

    This google architecture components github thread has more info on this misleading error, but not sure if the issue has been fixed yet.

    0 讨论(0)
  • 2020-12-01 09:27

    Had a similar issue before.

    First I've updated/added apply plugin: 'kotlin-kapt' to gradle.

    Next, I've used it instead of annotationProcessor in gradle:

    kapt "android.arch.persistence.room:compiler:1.0.0-alpha4"

    Tha last thing was to create an immutable data class:

    @Entity(tableName = "movies")
    data class MovieKt(
        @PrimaryKey
        val id : Int,
        val title: String,
        val overview: String,
        val poster_path: String,
        val backdrop_path: String,
        val release_date: String,
        val vote_average: Double,
        val isFavorite: Int
    )
    

    UPDATE:

    This solution works when you have classes for the model and classes for Database in the same Android Module. If you have model classes in Android Library module and the rest of the code in your main module, Room will NOT recognize them.

    0 讨论(0)
  • 2020-12-01 09:27

    I also had this issue, but i realized the problem was that i added the @Embedded annotation to a property that already had a type converter, so anyone having the same problem should check the property declarations for your model class carefully and make sure the @Embedded annotation is not on a property that has a type converter associated with it.

    0 讨论(0)
  • 2020-12-01 09:27

    With 2.1.0-alpha6, it turned out to be an invalid return type in Dao. Fixing the return type as expected fixed it.

    0 讨论(0)
  • 2020-12-01 09:29

    Just add the below annotation to any constructor that causes the errors and add a new blank constructor.

    @Ignore

    0 讨论(0)
提交回复
热议问题