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

前端 未结 24 1754
长发绾君心
长发绾君心 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:30

    It turned out to be a bug on the library https://github.com/googlesamples/android-architecture-components/issues/49

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

    https://issuetracker.google.com/issues/62851733

    i found this is @Relation's projection bug! not Kotlin language problem. based google GithubBrowserSample java also happend error, but different error message.

    below is my kotlin code:

    data class UserWithCommunities(
            @Embedded
            var user: User = User(0, null),
    
            @Relation(parentColumn = "id",
                    entityColumn = "users_id",
                    entity = CommunityUsers::class,
                    projection = arrayOf("communities_id")) // delete this line.
            var communityIds: List<Int> = emptyList()
    )
    

    right:

    data class UserWithCommunities(
            @Embedded
            var user: User = User(0, null),
    
            @Relation(parentColumn = "id",
                    entityColumn = "users_id",
                    entity = CommunityUsers::class)
            var communityList: List<CommunityUsers> = emptyList()
    )
    
    0 讨论(0)
  • 2020-12-01 09:31

    Kotlin plugin doesn't pick up annotationProcessor dependencies, So use the latest version of Kotlin annotation processor - put this line at top of your module's level build.gradle file

    apply plugin: 'kotlin-kapt'

    like

    apply plugin: 'kotlin-android'
    apply plugin: 'kotlin-android-extensions'
    apply plugin: 'kotlin-kapt'  // add this line
    
    android {
        compileSdkVersion 28
        defaultConfig {
          ........
        }
    }
    

    Don't forget to change the compileSdkVersion accordingly.

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

    you need to specify a secondary constructor like so:

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

    In my case I wasn't returning type in one of the Dao query hope it helps others Thanks

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

    To expand on the answers provided by @evanchooly and @daneejela, you need a secondary constructor to be able to use @Ignore parameters in your primary constructor. This is so Room still has a constructor that it can use when instantiating your object. Per your example, if we ignore one of the fields:

    @Entity(tableName = "movies")
    data class MovieKt(
            @PrimaryKey
            var id : Int,
            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
    ) {
        constructor(id: Int, title: String, overview: String, poster_path: String, backdrop_path: String) {
            this(id, title, overview, poster_path, backdrop_path, "", 0.0, 0)
        }
    }
    
    0 讨论(0)
提交回复
热议问题