【发布时间】:2021-01-24 19:18:17
【问题描述】:
在我的 DAO 中,与由 Hilt 注入到 ViewModel 的房间相关联,并带有一个数据类实体 “FoodMenu”包含一组对象,这些对象又使用类型转换器在使用 @Query 时显示错误,但与 @Insert 或 @Delete 一起工作正常。
我的道
@Dao
interface MenuDAO {
@Insert(onConflict = OnConflictStrategy.REPLACE)
suspend fun addMenu(element:FoodMenu):Unit
//If i comment out the below @Query..... section. There is no error
@Query("SELECT * FROM menu")
fun getMenu():List<FoodMenu>
}
应用数据库
@Database (entities=[Cart::class,FoodMenu::class],version = 1,exportSchema = false)
@TypeConverters(TypeConverter::class)
abstract class AppDatabase : RoomDatabase() {
abstract fun menuDAO():MenuDAO
}
数据类和类型转换器
data class Food(
var name:String,
var price:Int,
var image:String,
)
@Entity(tableName="menu")
data class FoodMenu(
@PrimaryKey
var category:String,
var list:List<Food>
)
class TypeConverter{
@TypeConverter
fun fromFoodList(value:List<Food>?)= Gson().toJson(value)
@TypeConverter
fun toFoodList(value:String)= Gson().fromJson(value,Array<Food>::class.java).toList()
}
我的错误
> Task :app:kaptDebugKotlin FAILED
Execution failed for task ':app:kaptDebugKotlin'.
> A failure occurred while executing org.jetbrains.kotlin.gradle.internal.KaptExecution
> java.lang.reflect.InvocationTargetException (no error message)
【问题讨论】:
标签: java android kotlin android-room dagger-hilt