javagoogle-cloud-datastoreobjectify

Unable to Save Objectify entity with Map<string,Object> in Embedded classes?


I have an embedded entity that has a Field of type Map<String, Object>.

All keys to the Map Are of type String.

Below is Entity

@Entity
@Index
@Getter @Setter
public class NiftySurveys {
    @Id
    private Long id;
    private List<SurveyQuestions> questions;
    private Long createddate;
    private Long updatedDate;
}

@Getter @Setter
public class SurveyQuestions {
    private String label;
    private String code;
    private SurveyQuestionGroups questionGroup;
    private Map<String,Object> optionGroup;
}

I am having trouble saving Entity with optionGroup.

Sample Entity submitted From FrontEnd

{
    "questions": [{
        "label": "jio",
        "code": null,
        "questionGroup": {
            "name": "Date Time",
            "value": "DATI"
        },
        "optionGroup": {
            "labels": [{
                "label": "Date / Time"
            }],
            "collectDateInfo": true,
            "collectTimeInfo": true,
            "dateFormat": "MM/DD/YYYY",
            "validationMessage": "Please Enter a Valid Date!"
        }
    }, {
        "code": null,
        "label": "Q2",
        "questionGroup": {
            "name": "Multiple Choice Questions",
            "value": "MCQ"
        },
        "optionGroup": {
            "name": "Agree - Disagree",
            "code": "AGDAG",
            "options": [{
                "label": "YES",
                "value": "Y"
            }, {
                "label": "NO",
                "value": "N"
            }]
        }
    }]
}

All Keys of the map Are Strings.

Error Message:

exception: "com.googlecode.objectify.SaveException"
message: "Error saving com.nifty.niftyfeedbacks.domain.NiftySurveys@4ac88d5e: java.lang.IllegalStateException: Embedded Map keys must be of type String/Enum/Key<?> or field must specify @Stringify"

link to Stack Trace https://drive.google.com/file/d/1fqpPLiJutWLif5GnrlqLEZ6Wr_PdLdC-/view?usp=sharing


Solution

  • Use @Serialize annotation for complex object graph. From Objectify Docs

    This Solved my issue.

    @Getter @Setter
    public class SurveyQuestions implements Serializable{
        private static final long serialVersionUID = -6930992373082651231L;
        @Serialize
        private Map<String,Object> optionGroup;
        
        
    }