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<
It turned out to be a bug on the library https://github.com/googlesamples/android-architecture-components/issues/49
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()
)
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.
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)
}
In my case I wasn't returning type in one of the Dao query hope it helps others Thanks
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)
}
}