Room Persistence @Relation working in Java but not in Kolin

匿名 (未验证) 提交于 2019-12-03 03:05:02

问题:

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:

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 } 

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?

回答1:

Try to update room to 1.0.0-alpha8 and add List<@JvmSuppressWildcards Pet>.



回答2:

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

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

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.

I hope this will resolve the issue



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!