Search code examples
javafirebasegoogle-cloud-firestorejava-record

Firebase and Using Records Structure on Java 17


I am facing an issue, I wanted to learn from it if anyone has a solution.

I updated my system from Java 11 to 17 and changed some of my POJO from a normal class with setters/getters to records (first introduced on Java 14).

Problem now is when trying to store these POJOs on my Firestore database, it throws the exception "java.lang.RuntimeException: No properties to serialize found on class XXX",

I understand, it makes sense because a Record in Java doesn't have the concept of setters as they are immutable therefore the fields are set once the POJO is created using the all Args constructor but when Firestore does the mapping to a document reference, it does it via setters, hence the exception.

Conclusion, I had to go back to normal classes for POJOs where I can have the setters instead of using records, but I keep thinking there should be a way to use Records, maybe way to change the how Firestore makes the mappings... any suggestion?

Thanks


Solution

  • The Firebase Android SDK has built-in support for serializing plain-old Java objects that adhere to JavaBean conventions only. It indeed uses run-time type information to inspect such objects and map their properties to/from the snapshots in the database.

    The Firebase SDKs have no built-in knowledge of Java Record classes. If you want to serialize/deserialize such classes you'll have to write code for that yourself, outside of the Firebase SDK.