我愿意尝试来自 Android 的新 Room Library,但遇到了以下错误:
错误:(19, 29) 错误:无法弄清楚如何将此字段保存到数据库中。您可以考虑为其添加类型转换器。
此错误涉及以下类成员:
private HashSet<String> fruits;
我有以下课程:
@Entity(tableName = "SchoolLunches")
public class SchoolLunch {
@PrimaryKey(autoGenerate = true)
private int lunchId;
private boolean isFresh;
private boolean containsMeat;
private HashSet<String> fruits;
public int getLunchId() {
return lunchId;
}
public void setLunchId(int lunchId) {
this.lunchId = lunchId;
}
public boolean isFresh() {
return isFresh;
}
public void setFresh(boolean fresh) {
isFresh = fresh;
}
public boolean isContainsMeat() {
return containsMeat;
}
public void setContainsMeat(boolean containsMeat) {
this.containsMeat = containsMeat;
}
public HashSet<String> getFruits() {
return fruits;
}
public void setFruits(HashSet<String> fruits) {
this.fruits = fruits;
}
此外,还有一个相对的 DAO 类:
@Dao
public interface SchoolLunchDAO {
@Query("SELECT * FROM SchoolLunches")
List<SchoolLunch> getAll();
@Insert
void insertAll(SchoolLunch... schoolLunches);
@Query("DELETE FROM SchoolLunches")
void deleteAll();
}
由于我正在努力成为一名非常优秀的开发人员,因此我编写了一个单元测试如下:
@Test
public void singleEntityTest() {
HashSet<String> fruitSet = new HashSet<>();
fruitSet.add("Apple");
fruitSet.add("Orange");
SchoolLunch schoolLunch = new SchoolLunch();
schoolLunch.setContainsMeat(false);
schoolLunch.setFresh(true);
schoolLunch.setFruits(fruitSet);
schoolLunchDAO.insertAll(schoolLunch);
List<SchoolLunch> schoolLunches = schoolLunchDAO.getAll();
assertEquals(schoolLunches.size(), 1);
SchoolLunch extractedSchoolLunch = schoolLunches.get(0);
assertEquals(false, extractedSchoolLunch.isContainsMeat());
assertEquals(true, extractedSchoolLunch.isFresh());
assertEquals(2, extractedSchoolLunch.getFruits().size());
}
我应该在这里做什么?