October 21, 2024
Chicago 12, Melborne City, USA
java

java mongo driver – how do I save entity to mongo that does not serialize with getters


I use java mongo driver to access mongo.

I have this example entity class:

public class TestJavaMongoDriverEntityMDO {
    
    @BsonProperty("_id")
    private ObjectId id;
    
    @BsonProperty("val")
    private String val;

    public TestJavaMongoDriverEntityMDO() {
        this.val = null;
    }

    public TestJavaMongoDriverEntityMDO(String val) {
        this.val = val;
    }

    public String getVal() {
        if (val == null) {
            return "someValue";
        }
        return val;
    }

    public void setVal(String val) {
        this.val = val;
    }

    public ObjectId getId() {
        return id;
    }
    
    public void setId(ObjectId id) {
        this.id = id;
    }
}

when I save It to the database with val == null then the result object in mongo has value val set to "someValue"

what should I do to serialize the field val as it is – so null, rather than the result of the get action?

thank you



You need to sign in to view this answers

Leave feedback about this

  • Quality
  • Price
  • Service

PROS

+
Add Field

CONS

+
Add Field
Choose Image
Choose Video