I upgraded to Firebase 9.0.2. When saving to the Realtime Database I cannot get objects to map like they used to in older versions. See my sample project demonstrating this.
For instance, take these objects:
public class Vehicle {
private String vin;
private int wheels;
private int color;
private double price;
private String type;
public Vehicle() { }
....
//public accessors
....
}
and
public class Truck extends Vehicle {
private int bedLength;
private boolean fourWheelDrive;
private boolean diesel;
public Truck() { }
....
//public accessors
....
}
If I were to create and save a Truck
object:
final Truck truck = new Truck();
truck.setVin(getRef().push().getKey());
truck.setVehicleType("truck");
truck.setColor(random.color());
truck.setPrice(random.price());
truck.setBedLength(random.number(8));
truck.setDiesel(random.bool());
truck.setFourWheelDrive(random.bool());
truck.setVehicleType(VehicleType.TRUCK);
FirebaseDatabase
.getInstance()
.getReference()
.child(vehicle.getVin())
.setValue(truck);
I only see values from Truck
, and none from Vehicle
:
"-KKpeYZO4eG3lAYyCd40" : {
"bedLength" : 1,
"diesel" : true,
"fourWheelDrive" : true
}
I am considering making a JSON mapper and manually map all of my objects every time I save or retrieve(Like older versions of Firebase used to do). If there are better solutions or plans to fix this I would really like to know before I start down that rabbit hole.
In the meantime, I have followed the advice from this answer, which that makes for ugly, redundant, and bug-prone code.
Edit:
This solution from Firebase engineer, Frank van Puffelen did not work for me because I believe that it is geared towards deserializing objects, where I am struggling with serializing.