Adding data to Firebase using custom objects

I’m fairly new to using Firebase, and their nosql type databases. It did take me some time to understand how data is arranged, with collections and documents.  When developing my application, in Java, I created a class for storing data, a.k.a. POJO. What is unclear from the Firebase documentation is how the fields in a document will be named.

There is a line that says this: Each custom class must have a public constructor that takes no arguments. In addition, the class must include a public getter for each property.

I initially did not pay attention to this public getter, i.e. a get method for each property in my object. Of course it is good practice to always create all the methods when creating a class. I was lazy, thinking that I only add them when I need to use them. Then came the interesting bit, where even after all the properties in my object had been initialised, and added to the Firebase document, some properties went missing in the database. I found it very odd. I initially thought that the fields in a Firebase document would be added according to my Java class, and following the naming I used in my class. But that was not the case. Dug a bit deeper, and it follows that if my getter method is getImgPath(), then the field in my Firebase document will become imgPath (note the upper case for “I” becomes “i”).  And without a getter method for property, then you will have a missing field in your Firebase document!

Hope this helps for those who are wondering how a document in Firebase is populated using custom objects.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.