0

I have the following class:

package com.mikhailovskii.trakttv.data.entity

import androidx.room.Entity
import androidx.room.Ignore
import androidx.room.PrimaryKey
import androidx.room.TypeConverters

import com.google.gson.annotations.Expose
import com.google.gson.annotations.SerializedName
import com.mikhailovskii.trakttv.db.room.MovieIdConverter

@Entity
class Movie {

    @PrimaryKey(autoGenerate = true)
    var id: Int = 0

    @SerializedName("ids")
    @Expose
    @TypeConverters(MovieIdConverter::class)
    var movieId: MovieId? = null

    @SerializedName("title")
    @Expose
    var name: String? = null

    @SerializedName("year")
    @Expose
    var year: Int = 0

    @SerializedName("tagline")
    @Expose
    var tagline: String? = null

    @SerializedName("released")
    @Expose
    var released: String? = null

    @SerializedName("runtime")
    @Expose
    var runtime: Int = 0

    @SerializedName("country")
    @Expose
    var country: String? = null

    @SerializedName("overview")
    @Expose
    var overview: String? = null

    var iconUrl: String? = null

    var watchers: Int = 0

    //For movie list
    @Ignore
    constructor(iconUrl: String, name: String, year: Int, slugId: String, watchers: Int) {
        this.iconUrl = iconUrl
        this.name = name
        this.year = year
        this.movieId?.slug = slugId
        this.watchers = watchers
    }

    //For movie detail
    @Ignore
    constructor(iconUrl: String, name: String, year: Int, tagline: String, released: String, runtime: Int, country: String, overview: String, slugId: String, watchers: Int) {
        this.iconUrl = iconUrl
        this.name = name
        this.year = year
        this.tagline = tagline
        this.released = released
        this.runtime = runtime
        this.country = country
        this.overview = overview
        this.movieId?.slug = slugId
        this.watchers = watchers
    }

    //For room
    constructor(name: String, watchers: Int, iconUrl: String, slugId: String) {
        this.iconUrl = iconUrl
        this.movieId?.slug = slugId
        this.watchers = watchers
        this.name = name
    }

}

This class is used for several aims, such as parsing JSON Object, adding data to Room DB and just for creating objects. And I faced with the following problem:

error: Entities and Pojos must have a usable public constructor. You can have an empty constructor or a constructor whose parameters match the fields (by name and type).

public final class Movie {
             ^

I looked on the previous questions about the same problem (especially this) and as I understand, my problem is in this line in constructors:

this.movieId?.slug = slugId 

Names aren't matched. but there are no opportunities to match this names, as I see, so, how can I solve this problem?

Sergei Mikhailovskii
  • 2,100
  • 2
  • 21
  • 43
  • Have u check this https://stackoverflow.com/questions/44485631/room-persistence-errorentities-and-pojos-must-have-a-usable-public-constructor and this https://www.google.com/search?q=error:+Entities+and+Pojos+must+have+a+usable+public+constructor.+Kotlin+site:stackoverflow.com&safe=strict&sa=X&ved=2ahUKEwi6wdnZ2ffiAhVBeH0KHWIkDTIQrQIoBDAAegQIBhAM&biw=1440&bih=761 – AskNilesh Jun 20 '19 at 09:03
  • @NileshRathod, yes, I've already checked it – Sergei Mikhailovskii Jun 20 '19 at 09:08

2 Answers2

1

You need empty constructor or all parameters constructor. So if you add constructor(), problem is solved

Dino
  • 26
  • 1
  • Thanks a lot! Probably I still can't understand how does it work, as when I add data to db I use not empty constructor, but thanks that you made it working :) – Sergei Mikhailovskii Jun 20 '19 at 09:38
1

So you don't need to add empty constructor manually to all entities, you can use a compiler plugin:

plugins {
  id "org.jetbrains.kotlin.plugin.noarg" version "1.3.31"
}

noArg {
    annotation("androidx.room.Entity")
}
Alexey Romanov
  • 167,066
  • 35
  • 309
  • 487