简体   繁体   中英

How to save a Enum data in a POJO file to MongoDB

I am able to save a Simple POJO file content in MongoDB using Gson, but now I want to save the enum data inside POJO file to MongoDB. But I am not getting how to save the enum data.

This is my POJO file:

import javax.annotation.Generated;

@Generated("org.jsonschema2pojo")
public class Coverage1 {
    public  enum Coverage {
Hearing_Aid_Professional_Liability("HEAR"), Incidental_Motorized_Land_Conveyances_Liability_Only("LANDC"), PremisesOperations_334("PREM"), Rental_Reimbursement("RREIM"), Liquor_Law_Liability_332("LLL"), Wind("WIND"), Business_Personal_Property("BPP"), OpticianOptometrists_Professional_Liability("OOPRL"), Builders_Risk("BLDRK");

    private String val;

Coverage(String val){
        this.val = val;
    }

    public String getVal ()
    {
        return this.val;
    }

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


    private String id;

    private CoverageCd coverageCd;
    private CoverageDesc coverageDesc;
    private CoverageTypeCd coverageTypeCd;

    public String getId() {
        return id;
    }


    public void setId(String id) {
        this.id = id;
    }


    public CoverageCd getCoverageCd() {
        return coverageCd;
    }


    public void setCoverageCd(CoverageCd coverageCd) {
        this.coverageCd = coverageCd;
    }


    public CoverageDesc getCoverageDesc() {
        return coverageDesc;
    }

    public void setCoverageDesc(CoverageDesc coverageDesc) {
        this.coverageDesc = coverageDesc;
    }


    public CoverageTypeCd getCoverageTypeCd() {
        return coverageTypeCd;
    }

    public void setCoverageTypeCd(CoverageTypeCd coverageTypeCd) {
        this.coverageTypeCd = coverageTypeCd;
    }

}

This is my class from where I am making Mongo Call

       Employee employee = new Employee(); // Create java object of Simple POJO with field No and Name
        employee.setNo(2L);
        employee.setName("POJO Test");
        Coverage1 cv= new Coverage1();//POJO containing Enum
        //How to save the Enum in Mongo
        // Deserialize object to json string
        Gson gson = new Gson();
        String json = gson.toJson(employee);
        System.out.println(json);
        // Parse to bson document and insert
       Document doc = Document.parse(json);
       db.getCollection("NameColl").insertOne(doc);

I am now able to get the values of Enum in another class, but not getting how to save the entire data in MongoDB.

Coverage1 cv= new Coverage1();
            for(Coverage1.Coverage enumval:Coverage1.Coverage.values()){
                System.out.println(enumval);
                cv.setValue(enumval);//How to set the entire Enum Data in Mongo
            }

Please suggest how can i insert the entire Enum data in MongoDB.

You can save the constant as String calling the Enum#name() method and retrieve it back using Enum#valueOf()

Example:

myEnum.valueOf(arg0)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM