Android 持久化室:“无法弄清楚如何从游标中读取此字段" [英] Android Persistence room: "Cannot figure out how to read this field from a cursor"
问题描述
我正在尝试使用新的 Android Persistence Room Library 在两个数据库表之间创建关系.我查看了文档并尝试实现在
3) UserWithPets POJO:
//注意:这个类定义不需要注解.公共类 UserWithPets {@嵌入式公共用户用户;@Relation(parentColumn = "id", entityColumn = "userId", entity = Pet.class)公共列表<宠物>宠物;//或简单地使用列出宠物;"/* 或者,您可以使用投影从相关的 Pet 表中获取特定列(即仅宠物的名称).您可以取消注释并在下面尝试;@Relation(parentColumn = "id", entityColumn = "userId", entity = Pet.class,projection = "name")公共列表<字符串>宠物;*/}
parentColumn
指的是内嵌的User
表的id
列,entityColumn
指的是Pet
表的userId
(User
-Pet
关系)列,entity
指的是与User
表有关系的表(Pet
).
4) 用户道:
@Dao公共接口 UserDao {@Query("SELECT * FROM 用户")公共列表loadUsersWithPets();}
现在尝试 loadUsersWithPets()
,它会返回用户及其宠物列表.
请参阅我的其他答案 对于许多关系.
I'm trying to create a relation between two database tables using the new Android Persistence Room Library. I looked at the documentation and tried to implement the example found at 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 {
@Query("SELECT * from User")
public List<UserAllPets> loadUserAndPets();
}
I get the following error
...error: Cannot figure out how to read this field from a cursor.
in relation to:
private java.util.List<?> pets;
I would like to point out that I found some things in their docs really confusing. For example the lack of @PrimaryKey
and also the fact that the User
class is missing the @Entity
annotation, although it's supposed to be an entity (as fas as I see it). Did anybody run into the same problem? Thanks a lot in advance
Document is really confusing. Try with just below classes:
1) User Entity:
@Entity
public class User {
@PrimaryKey
public int id; // User id
}
2) Pet Entity:
@Entity
public class Pet {
@PrimaryKey
public int id; // Pet id
public int userId; // User id
public String name;
}
3) UserWithPets POJO:
// Note: No annotation required at this class definition.
public class UserWithPets {
@Embedded
public User user;
@Relation(parentColumn = "id", entityColumn = "userId", entity = Pet.class)
public List<Pet> pets; // or use simply 'List pets;'
/* Alternatively you can use projection to fetch a specific column (i.e. only name of the pets) from related Pet table. You can uncomment and try below;
@Relation(parentColumn = "id", entityColumn = "userId", entity = Pet.class, projection = "name")
public List<String> pets;
*/
}
parentColumn
refers to EmbeddedUser
table'sid
column,entityColumn
refers toPet
table'suserId
(User
-Pet
relation) column,entity
refers to table(Pet
) which has relation withUser
table.
4) UserDao Dao:
@Dao
public interface UserDao {
@Query("SELECT * FROM User")
public List<UserWithPets> loadUsersWithPets();
}
Now try loadUsersWithPets()
, which returns the users with their list of pets.
Edit: See my other answer for many ot many relation.
这篇关于Android 持久化室:“无法弄清楚如何从游标中读取此字段"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!