Room Persistence @Relation 在 Java 中工作但在 Kotlin 中无效 [英] Room Persistence @Relation working in Java but not in Kotlin

查看:27
本文介绍了Room Persistence @Relation 在 Java 中工作但在 Kotlin 中无效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基于我之前的问题(Android 持久化室:无法弄清楚如何从光标读取此字段"),感谢反馈,我开始工作,我在 Kolin 中实现了相同的示例(请参阅下面的代码).我不得不做一些小改动,比如现在传递给查询的参数,这些参数必须作为p0"、p1"等传递.现在在 Kotlin 中,我收到与 UserWithPets 类相关的以下错误:

Base on my previous question (Android Persistence room: "Cannot figure out how to read this field from a cursor") which I got to work thanks the feedback, I implemented the same example in Kolin (see code below). I had to make some minor changes like the parameters that are now passed to a the query which have to be passed as "p0", "p1" etc. Now in Kotlin I get the following error related to the UserWithPets class:

错误:无法弄清楚如何从游标读取此字段.e: 私有 java.util.List pets;

@Dao
interface UserDAO {   

    @get:Query("SELECT * FROM user")
    val all: LiveData<List<User>>

    @Insert
    fun insertUser(user: User) //single one

    @Insert(onConflict = OnConflictStrategy.REPLACE)
    fun insertUsers(vararg users: User)

    @Query("SELECT * FROM User")
    fun loadUsersWithPets(): LiveData<List<UserWithPets>>

}


@Entity
class Pet( var name: String?,  var ownerId: Int,@PrimaryKey(autoGenerate = true)var id:Int)



@Dao
interface PetDAO {
    @Query("SELECT * FROM pet")
    val all: List<Pet>

    @Query("SELECT * FROM pet WHERE id IN (:p0)")
    fun loadAllByIds(petIds: IntArray): List<Pet>


    @Insert
    fun insert(pet: Pet)

    @Insert
    fun insertAll(vararg pets: Pet)

    @Delete
    fun delete(user: Pet)
}


class UserWithPets {
    @Embedded
    var user: User? = null

    @Relation(parentColumn = "id", entityColumn = "ownerId", entity = Pet::class)
    var pets: List<Pet>? = null
}

看来,如果我用 Java 编写 UserWithPets 类,它会工作正常,但在用 Kotlin 编写时会失败.任何想法有什么问题?这是注释处理问题吗?

It appears that if I write the UserWithPets class in Java it will work fine, but fails when it is written in Kotlin. Any ideas what is wrong? Is this an annotation processing issue?

推荐答案

尝试更改您的查询 @Query("SELECT * FROM pet WHERE id IN (:p0)")

try changing your query @Query("SELECT * FROM pet WHERE id IN (:p0)")

@Query("SELECT * FROM pet WHERE id IN (:petIds)")

kotlin 和 Room lib 之间存在问题,Kotlin 没有正确保留参数的实际参数名称,因此 (:p0)/(:arg0) 解决了这个问题,现在已解决.

There was an issue between kotlin and Room lib, Kotlin wasn't preserving the actual parameter names of the arguments properly, so (:p0)/(:arg0) was work around that, its resolved now.

希望这能解决问题

这篇关于Room Persistence @Relation 在 Java 中工作但在 Kotlin 中无效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆