我正在尝试从Cloud Firestore数据库中检索一组复杂的对象。这是我的课:
public class Book {
private String id;
private String name;
private String original;
private ArrayList<Author> authors;
private ArrayList<Translator> translators;
private Series series;
private Float no;
private String seriesNo;
private Integer published;
private Borrower borrower;
private Boolean isCollection;
private Status status;
}
将此Book对象保存到数据库的最佳方法是什么?我目前正在使用以下方法。
public void createBook(String userId) {
FirebaseFirestore firebaseFirestore = FirebaseFirestore.getInstance();
DocumentReference documentReference = firebaseFirestore.collection("books").document();
Map<String, Object> newBook = new HashMap<>();
newBook.put("owner", userId);
newBook.put("id", documentReference.getId());
newBook.put("name", getName());
newBook.put("original", getOriginal());
if (getSeries() != null) {
Map<String, Object> series = new HashMap<>();
series.put("id", getSeries().getId());
series.put("name", getSeries().getName());
newBook.put("series", series);
}
newBook.put("no", getNo());
newBook.put("seriesNo", getSeriesNo());
newBook.put("published", getPublished());
if (getBorrower() != null) {
Map<String, Object> borrower = new HashMap<>();
borrower.put("id", getBorrower().getId());
borrower.put("nameInEnglish", getBorrower().getNameInEnglish());
borrower.put("nameInSinhalese", getBorrower().getNameInSinhalese());
borrower.put("mobile", getBorrower().getMobile());
newBook.put("borrower", borrower);
}
newBook.put("isCollection", getIsCollection());
newBook.put("status", getStatus().toString());
从数据库中检索保存的值的最佳方法是什么?很难找到一种同时访问所有值的好方法。我目前使用以下方法来检索除 Authors 和 Translators(它们是 ArrayLists)之外的值。
相关分类