我正在尝试使用新的Android Persistence Room Library在两个数据库表之间创建关系.我查看了文档,并尝试实现https://developer.android.com/reference/android/arch/persistence/room/Relation.html上的示例:
@Entity
public class User {
@PrimaryKey
int id;
}
@Entity
public class Pet {
@PrimaryKey
int id;
int userId;
String name;
}
@Dao
public interface UserDao {
@Query("SELECT * from User")
public List<User> loadUser();
}
@Dao
public interface PetDao {
@Query("SELECT * from Pet")
public List<Pet> loadUserAndPets();
}
public class UserAllPets {
@Embedded
public User user;
@Relation(parentColumn = "user.id", entityColumn = "userId", entity = Pet.class)
public List pets;
}
@Dao
public interface UserPetDao { …
Run Code Online (Sandbox Code Playgroud) 基于我之前的问题(Android持久性空间:"无法弄清楚如何从光标读取此字段")我得到了工作,感谢反馈,我在Kolin中实现了相同的示例(请参阅下面的代码).我不得不做一些小改动,比如现在传递给查询的参数必须作为"p0","p1"等传递.现在在Kotlin中我得到以下与UserWithPets类相关的错误:
错误:无法弄清楚如何从游标中读取此字段.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 …
Run Code Online (Sandbox Code Playgroud)