Android 持久空间:"Cannot figure out how to read this field from a cursor"
Android Persistence room: "Cannot figure out how to read this field from a cursor"
我正在尝试使用新的 Android Persistence Room 库在两个数据库表之间创建关系。我查看了文档并尝试实现在 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();
}
我收到以下错误
...error: Cannot figure out how to read this field from a cursor.
关于:
private java.util.List<?> pets;
我想指出的是,我发现他们文档中的某些内容确实令人困惑。例如缺少 @PrimaryKey
以及 User
class 缺少 @Entity
注释的事实,尽管它应该是一个实体(正如我所看到的那样).有人 运行 遇到同样的问题吗?非常感谢
Document 真是令人费解。试试下面 类:
1) 用户实体:
@Entity
public class User {
@PrimaryKey
public int id; // User id
}
2) 宠物实体:
@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
指嵌入式 User
table 的 id
列,
entityColumn
指的是 Pet
table 的 userId
(User
- Pet
关系)列,
entity
指的是 table(Pet
) 与 User
table. 有关系
4) UserDao道:
@Dao
public interface UserDao {
@Query("SELECT * FROM User")
public List<UserWithPets> loadUsersWithPets();
}
现在尝试 loadUsersWithPets()
,其中 returns 用户及其宠物列表。
编辑: 请参阅我的 other answer 了解许多关系。
我正在尝试使用新的 Android Persistence Room 库在两个数据库表之间创建关系。我查看了文档并尝试实现在 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();
}
我收到以下错误
...error: Cannot figure out how to read this field from a cursor.
关于:
private java.util.List<?> pets;
我想指出的是,我发现他们文档中的某些内容确实令人困惑。例如缺少 @PrimaryKey
以及 User
class 缺少 @Entity
注释的事实,尽管它应该是一个实体(正如我所看到的那样).有人 运行 遇到同样的问题吗?非常感谢
Document 真是令人费解。试试下面 类:
1) 用户实体:
@Entity
public class User {
@PrimaryKey
public int id; // User id
}
2) 宠物实体:
@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
指嵌入式User
table 的id
列,entityColumn
指的是Pet
table 的userId
(User
-Pet
关系)列,entity
指的是 table(Pet
) 与User
table. 有关系
4) UserDao道:
@Dao
public interface UserDao {
@Query("SELECT * FROM User")
public List<UserWithPets> loadUsersWithPets();
}
现在尝试 loadUsersWithPets()
,其中 returns 用户及其宠物列表。
编辑: 请参阅我的 other answer 了解许多关系。