房间db中对象列表的类型转换器

时间:2019-06-20 12:00:06

标签: android kotlin android-room

我想在我的房间数据库中插入每个任务的雇员列表。我收到此错误:

  

无法弄清楚如何将该字段保存到数据库中。您可以   考虑为其添加类型转换器。私人决赛   java.util.List被分配者= null;

如何为列表创建类型转换器?

 @Entity(tableName = "Task",
     foreignKeys = [(
            ForeignKey(entity = IssueType::class,
                    onDelete = ForeignKey.CASCADE,
                    parentColumns = ["id"],
                    childColumns = ["typeId"]
            )),
        (ForeignKey(entity = Location::class,
                onDelete = ForeignKey.CASCADE,
                parentColumns = ["id"],
                childColumns = ["locationId"]))
    ])
 data class Task(
    @PrimaryKey(autoGenerate = true)
    val id: Int,
    val typeId: Id,
    val locationId: Id,
    val priority: Priority,
    val deadline: Date,
    val assignees: List<Employee>,
    val description: String,
    val taskState: TaskState
 )

1 个答案:

答案 0 :(得分:0)

@Entity(tableName = "EmployeeTask",
            primaryKeys = arrayOf("employeeId", "taskId"),
            foreignKeys = arrayOf(
                    ForeignKey(entity = Employee::class,
                            parentColumns = arrayOf("id"),
                            childColumns = arrayOf("employeeId")),
                    ForeignKey(entity = Task::class,
                            parentColumns = arrayOf("id"),
                            childColumns = arrayOf("taskId"))
            )
    )
    data class EmployeeIssue(val employeeId: Id,
                             val taskId: Id)



     @Dao
        interface EmployeeWithTaskDao {
            @Insert
            fun insert(employedTask: EmployeeTask)

            @Query("""
                   SELECT * FROM Task
                   INNER JOIN EmployeeTask
                   ON Task.id=EmployeeTask.taskId
                   WHERE EmployeeTask.employeeId=:employeeId
                   """)
            fun getTasksForEmployees(employeeId: Int): Array<Task>

            @Query("""
                   SELECT * FROM employee
                   INNER JOIN EmployeeTask
                   ON EMPLOYEE.id=EmployeeTask.employeeId
                   WHERE EmployeeTask.taskId=:taskId
                   """)
            fun getEmployeesForTask(taskId: Int): Array<Employee>
        }