简体   繁体   English

Firestore - 究竟要放哪个@Exclude注释?

[英]Firestore - Where exactly to put the @Exclude annotation?

I am confused about where to put the @Exclude annotation for a field I don't want to have in my Cloud Firestore database. 我很困惑将@Exclude注释放在我不希望在Cloud Firestore数据库中使用的字段的哪个位置。

Is it enough to only put it on the getter method? 仅仅将它放在getter方法上就足够了吗? What effect does it have to also add it to the setter method or variable declaration? 它还有什么作用将它添加到setter方法或变量声明?

In my example, I don't want to store the document ID, since this would be redundant: 在我的示例中,我不想存储文档ID,因为这将是多余的:

public void loadNotes(View v) {
    notebookRef.get()
            .addOnSuccessListener(new OnSuccessListener<QuerySnapshot>() {
                @Override
                public void onSuccess(QuerySnapshot queryDocumentSnapshots) {
                    String data = "";

                    for (QueryDocumentSnapshot documentSnapshot : queryDocumentSnapshots) {
                        Note note = documentSnapshot.toObject(Note.class);
                        note.setDocumentId(documentSnapshot.getId());
                        String title = note.getTitle();
                        String description = note.getDescription();

                        data += "\nTitle: " + title + "Description: " + description;
                    }

                    textViewData.setText(data);
                }
            });
}

Model class: 型号类:

public class Note {
private String documentId;
private String title;
private String description;

public Note() {
    //public no arg constructor necessary
}

public Note(String title, String description) {
    this.title = title;
    this.description = description;
}

@Exclude
public String getDocumentId() {
    return documentId;
}

public void setDocumentId(String documentId) {
    this.documentId = documentId;
}

public String getTitle() {
    return title;
}

public String getDescription() {
    return description;
}

} }

Because you are using the private modifier for your fields inside your Note class, to exclude a property from a Cloud Firestore database you should place the @Exclude annotation before the corresponding getter. 因为你是使用private修改为您的内田Note类,以排除从云公司的FireStore数据库中的属性,你应该把@Exclude相应的getter之前的注释。

@Exclude
public String getDocumentId() {return documentId;}

Is it enough to only put it on the getter method? 仅仅将它放在getter方法上就足够了吗?

Yes, it is enough. 是的,这就够了。 If you have used the public modifier for your fields, to ignore a property you should have placed the @Exclude annotation before the property: 如果您已经为字段使用了public修饰符,要忽略属性,您应该在属性之前放置@Exclude批注:

@Exclude 
public String documentId;

What effect does it have to also add it to the setter method or variable declaration? 它还有什么作用将它添加到setter方法或变量声明?

No effect. 没有效果。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM