Reputation: 2465
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?
Upvotes: 0
Views: 877
Reputation: 170735
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")
}
Upvotes: 1
Reputation: 26
You need empty constructor or all parameters constructor.
So if you add constructor()
, problem is solved
Upvotes: 1