1

Using Room I am trying to fetch data from two tables (Company and Farm) into a single list. Using commonsware answer I created a base class CompanyFarmBase and two child classes Company and Farm. Now using the example I created Dao class with following code:

@Query("SELECT * FROM farm_table")
fun getAllFarm(): List<UserModel.Farm>

@Query("SELECT * FROM company_table")
fun getAllCompany(): List<UserModel.Company>

@Transaction
fun getAllCompanyFarm(): List<UserModel.CompanyFarmBase> {
    val result = ArrayList<UserModel.CompanyFarmBase>()

    result.addAll(getAllCompany())
    result.addAll(getAllFarm())

    return result
}

Now when I try to build I get these errors:

dao/FarmDao_Impl.java:100: error: illegal start of expression
  List<UserModel.CompanyFarmBase> _result = FarmDao.DefaultImpls.getAllCompanyFarm(this, );


dagger.internal.codegen.ComponentProcessor was unable to process com.navdeep.di.component.AppComponent because not all of its dependencies could be resolved. Check for compilation errors or a circular dependency with generated code.

Please let me know where I went wrong. Querying separately each table gives proper data. Thanks!

Navdroid
  • 1,541
  • 3
  • 25
  • 52

2 Answers2

0

Recently had this issue as well, passing in a default parameter will fix the issue. Why, no idea.

@Transaction
fun getAllCompanyFarm(notUsed: Boolean = true): List<UserModel.CompanyFarmBase> {
    val result = ArrayList<UserModel.CompanyFarmBase>()

    result.addAll(getAllCompany())
    result.addAll(getAllFarm())

    return result
}
Kory
  • 31
  • 1
  • 6
0

create separate data class to chain that models.

data class TaskWithActivities(
    @Embedded var taskEntity: TaskEntity, @Relation(
        parentColumn = "taskId",
        entityColumn = "taskpId"
    ) var activities: List<ActivityEntity>
)

inside room entity classes write relations: for a task (parent)

@Entity(
    tableName = "task_entity",
    primaryKeys = ["taskId", "mandantId"]
)
@kotlinx.parcelize.Parcelize
data class TaskEntity(

for activity:

@Entity(
    tableName = "activity_entity", indices = arrayOf(Index(value = ["taskpId", "mandantId"])),
    foreignKeys = [
        ForeignKey(
            entity = TaskEntity::class,
            parentColumns = ["taskId", "mandantId"],
            childColumns = ["taskpId", "mandantId"],
            onDelete = NO_ACTION
        )],
    primaryKeys = ["taskpId", "activityId", "mandantId"]
)

now you can request from two tables:

in your tasks DAO:

@Transaction
@Query("SELECT * FROM task_entity ORDER BY taskDueDateFinish ASC")
fun observeTaskWithActivities(): LiveData<List<TaskWithActivities>>
Anton Kogan
  • 153
  • 2
  • 10