Room Persistence @Relation在Java中起作用,但在Kolin中不起作用
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宠物;
error: Cannot figure out how to read this field from a cursor. e: private 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.
我希望这能解决问题