Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Room kotlin: Entities and Pojos must have a usable public constructor. Stuck on this error

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.

like image 579
Shudipto Trafder Avatar asked May 09 '18 13:05

Shudipto Trafder


People also ask

What are the constructor requirements for entities and POJOs?

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).

What is the error code for entities and POJOs?

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.

Why is my room annotation not working in Kotlin?

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?

Can I have an empty 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.


1 Answers

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>
}
like image 174
EpicPandaForce Avatar answered Sep 18 '22 09:09

EpicPandaForce