How To Append Additional Data Via The ToObject Method?

In this answer, and the user has appended something to the toObject method here:

PostModel changedModel = documentChange.getDocument().toObject(PostModel.class).withDocId(docID);

Where and how do I define the withDocId so that I can pass the docId? Is it in the POJO class?

My POJO class has this method:

public void withDocId(String docId) {
        this.docId = docId;


The following line of code:


Returns an object of type PostModel. When calling .withDocId(docID) on that object, it means that withDocId method is a method within that class.

Is it in the POJO class?

Yes it is.


how do I define it in the POJO class?

You can define a method in your POJO class in the same way you define the setters and getters. So it might be something line this:

public void withDocId(String docID) {
    //Your logic


In order to make this line of code compile:

PostModel changedModel = documentChange.getDocument().toObject(PostModel.class).withDocId(docID);

The .withDocId(docID) should return an object of type PostModel. So most likely the return type of your method should be PostModel. The above method was only an example. If you want to return a String, then you should change:

String docID = documentChange.getDocument().toObject(PostModel.class).withDocId(docID);