使用Room在单个事务中从多个表中获取数据? [英] Fetching data from multiple tables in single transaction using Room?
本文介绍了使用Room在单个事务中从多个表中获取数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
使用房间我正在尝试从两个表(公司和农场)中获取数据到一个列表中.使用通用软件,我创建了一个基类CompanyFarmBase和两个子类Company和Farm.现在使用示例,我使用以下代码创建了Dao类:
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!
推荐答案
最近也有此问题,传入默认参数将解决此问题.为什么,不知道.
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
}
这篇关于使用Room在单个事务中从多个表中获取数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文