Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

Is it enough to only put it on the getter method? What effect does it have to also add it to the setter method or variable declaration?

In my example, I don't want to store the document ID, since this would be redundant:

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;
}

}

like image 233
Florian Walther Avatar asked Apr 16 '18 20:04

Florian Walther


People also ask

Does firestore location matter?

Before you use Cloud Firestore, you must choose a location for your database. To reduce latency and increase availability, store your data close to the users and services that need it. This location setting is your project's default Google Cloud Platform (GCP) resource location.


1 Answers

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.

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

Is it enough to only put it on the getter method?

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:

@Exclude 
public String documentId;

What effect does it have to also add it to the setter method or variable declaration?

No effect.

like image 157
Alex Mamo Avatar answered Oct 18 '22 23:10

Alex Mamo