Java 如何从Firestore获取DocumentReference字段?
我正在尝试从google Firestore检索对象,这是对象:Java 如何从Firestore获取DocumentReference字段?,java,firebase,google-cloud-firestore,Java,Firebase,Google Cloud Firestore,我正在尝试从google Firestore检索对象,这是对象: @Data public class Car { private String plate; private String model; private long km; private boolean available; private DocumentReference soat; } 由于我添加了atribute soat im getting Errors im,因此我使
@Data
public class Car {
private String plate;
private String model;
private long km;
private boolean available;
private DocumentReference soat;
}
由于我添加了atribute soat im getting Errors im,因此我使用该类来反序列化对象:
@Data
public class Car {
private String plate;
private String model;
private long km;
private boolean available;
private DocumentReference soat;
}
这段代码用于从Firestore检索所有汽车对象
@Override
public List<Car> findAll() {
List<Car> empList = new ArrayList<Car>();
CollectionReference car = fb.getFirestore().collection("Cars");
ApiFuture<QuerySnapshot> querySnapshot = car.get();
try {
for (DocumentSnapshot doc : querySnapshot.get().getDocuments()) {
Car emp = doc.toObject(Car.class);
empList.add(emp);
}
} catch (Exception e) {
e.printStackTrace();
}
return empList;
}
编辑:刚刚尝试了这个方法,仍然得到相同的错误
@Override
public List<Car> findAll() {
List<Car> empList = new ArrayList<Car>();
CollectionReference car = fb.getFirestore().collection("Cars");
ApiFuture<QuerySnapshot> querySnapshot = car.get();
try {
for (DocumentSnapshot doc : querySnapshot.get().getDocuments()) {
String plate=doc.getData().get("plate").toString();
String model=doc.getData().get("model").toString();
long km=Long.parseLong(doc.getData().get("km").toString());
boolean available=Boolean.parseBoolean(doc.getData().get("available").toString());
DocumentReference soat=(DocumentReference)doc.getData().get("soat");
Car emp = new Car();
emp.setAvailable(available);
emp.setKm(km);
emp.setModel(model);
emp.setPlate(plate);
emp.setSoat(soat);
empList.add(emp);
}
} catch (Exception e) {
e.printStackTrace();
}
return empList;
}
现在我从Firestore电话里得到了这个
[{"plate":"HEO628","model":"2014","km":75000,"available":true,"soat":{"path":"SOATS/HEO628","parent":{"parent":null,"id":"SOATS","path":"SOATS","firestore":{"firestore":{"firestore":{"firestore":{"firestore":{"firestore":{"firestore":{"firestore":{"firestore":
基本上,您必须创建一个类来反序列化数据库引用,在这种情况下,类名将是
SOAT
,并使用它来取消对数据库的引用
@Data
public class Car {
private String plate;
private String model;
private long km;
private boolean available;
private SOAT soat;
private Insurance insurance;
private Techno techno;
}
然后,当从FireBase检索信息时,您必须发出以下请求,以指定要映射的对象的类型
public List<Car> findAll() {
List<Car> empList = new ArrayList<Car>();
CollectionReference car = fb.getFirestore().collection("Cars");
ApiFuture<QuerySnapshot> querySnapshot = car.get();
try {
for (DocumentSnapshot doc : querySnapshot.get().getDocuments()) {
String plate = doc.getData().get("plate").toString();
String model = doc.getData().get("model").toString();
long km = Long.parseLong(doc.getData().get("km").toString());
boolean available = Boolean.parseBoolean(doc.getData().get("available").toString());
DocumentSnapshot soatref = fb.getFirestore().collection("SOATS").document(plate).get().get();
DocumentSnapshot technoref = fb.getFirestore().collection("Technos").document(plate).get().get();
DocumentSnapshot insuranceref = fb.getFirestore().collection("Insurances").document(plate).get().get();
SOAT soat = soatref.toObject(SOAT.class);
Techno techno = technoref.toObject(Techno.class);
Insurance insurance = insuranceref.toObject(Insurance.class);
Car emp = new Car();
emp.setAvailable(available);
emp.setKm(km);
emp.setModel(model);
emp.setPlate(plate);
emp.setSoat(soat);
emp.setInsurance(insurance);
emp.setTechno(techno);
empList.add(emp);
}
} catch (Exception e) {
}
return empList;
}
您可能想尝试单独从快照中取出每个字段,而不是使用toObjut.To尝试,仍然得到相同的错误,感谢您的帮助,不管怎样,扫描显示代码,在那里你得到个人<代码> SOAT字段和在这种情况下得到的错误。我不确定这将如何抛出一个
stackoverflowerrror
caremp=doc.toObject(Car.class)代码>当我使用这部分代码时,它从第一张图像中显示的Firebase对象检索整个信息,并使用Car
类对其进行映射,因此我永远不会单独获得soat
。
public List<Car> findAll() {
List<Car> empList = new ArrayList<Car>();
CollectionReference car = fb.getFirestore().collection("Cars");
ApiFuture<QuerySnapshot> querySnapshot = car.get();
try {
for (DocumentSnapshot doc : querySnapshot.get().getDocuments()) {
String plate = doc.getData().get("plate").toString();
String model = doc.getData().get("model").toString();
long km = Long.parseLong(doc.getData().get("km").toString());
boolean available = Boolean.parseBoolean(doc.getData().get("available").toString());
DocumentSnapshot soatref = fb.getFirestore().collection("SOATS").document(plate).get().get();
DocumentSnapshot technoref = fb.getFirestore().collection("Technos").document(plate).get().get();
DocumentSnapshot insuranceref = fb.getFirestore().collection("Insurances").document(plate).get().get();
SOAT soat = soatref.toObject(SOAT.class);
Techno techno = technoref.toObject(Techno.class);
Insurance insurance = insuranceref.toObject(Insurance.class);
Car emp = new Car();
emp.setAvailable(available);
emp.setKm(km);
emp.setModel(model);
emp.setPlate(plate);
emp.setSoat(soat);
emp.setInsurance(insurance);
emp.setTechno(techno);
empList.add(emp);
}
} catch (Exception e) {
}
return empList;
}
{
"plate": "HEO628",
"model": "2014",
"km": 75000,
"available": true,
"soat": {
"expeditionDate": "2020-09-29T06:12:12.000+00:00",
"carPlate": "HEO628"
},
"insurance": {
"expeditionDate": "2020-10-01T11:12:12.000+00:00",
"carPlate": "HEO628"
},
"techno": {
"expeditionDate": "2020-09-08T17:00:00.000+00:00",
"carPlate": "HEO628"
}
}