I am getting two error
error: Fields annotated with @Relation cannot be constructor parameters. These values are fetched after the object is constructed.
error: Fields annotated with @Relation cannot be constructor parameters. These values are fetched after the object is constructed.
I have two table. One is task table and other is sub-task table. For every entity of task table, there are some entities of subtask table.
So I am trying to get all the task and the list of subtask fro individual task
Dao:
@Query("Select * from TaskTable")
fun getTasks():List<TaskModel>
TaskModel:
class TaskModel(
@Embedded
var taskTable: TaskTable,
@Relation(parentColumn = "id",
entityColumn = "taskId")
var subTaskTable: List<SubTaskTable>
)
TaskTable:
@Entity
data class TaskTable(
@PrimaryKey(autoGenerate = true)
var id:Int = 0,
var taskListId:Int = 0,
var title: String,
var details:String?
)
SubTaskTable:
@Entity
data class SubTaskTable (
@PrimaryKey(autoGenerate = true) var id: Int = 0,
var taskId: Int = 0,
var title: String,
var details: String?)
build.gradle
//room
def room_version = "1.1.0"
implementation "android.arch.persistence.room:runtime:$room_version"
kapt "android.arch.persistence.room:compiler:$room_version"
// Test helpers
testImplementation "android.arch.persistence.room:testing:$room_version"
And I apply kotlin-kapt plugin
Help me to fix this problem. I search on stack overflow and implement some answer but this not fixed.
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).
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). #364 Error:Entities and Pojos must have a usable public constructor.
This could also occur with a keyword clash with Java. For instance, the Java keyword static is allowed in Kotlin but when it is compiled down, it looks like they replace it with another name causing the Room annotation processor to not be able to make the match with the field. Can you post your DAO also?
You can have an empty constructor or a constructor whose parameters match the fields (by name and type). #364 Error:Entities and Pojos must have a usable public constructor.
Well it says they shouldn't be constructor parameters, so try making them not constructor parameters.
class TaskModel() {
@Embedded
lateinit var taskTable: TaskTable
@Relation(parentColumn = "id",
entityColumn = "taskId")
lateinit var subTaskTable: List<SubTaskTable>
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With