user_531
user_531

Reputation: 251

In spring boot java project how to map a property of different data types from a mongo DB to java class?

Hi i have a mongo DB ducument section which has the columns property as shown (1st format)

   "columns" : [
                    [
                        {
                            "itemId" : ObjectId("5b863b50083ae5eb1e678d75"), 
                            "type" : "field"
                        }
                    ], 
                    [
                        {
                            "itemId" : ObjectId("5b8d4404af0963f54e262f46"), 
                            "type" : "field"
                        }
                    ], 
                    [

                    ], 
                    [

                    ]
                ]


which is of the type Array of Array of Objects 

However at some places it is also stored in this format as well . (2nd format)


          "columns" : [
            {
                "0" : {
                    "itemId" : "5b863b50083ae5eb1e678d75", 
                    "type" : "field"
                }
            }, 
            {
                "0" : {
                    "itemId" : "5b8d4404af0963f54e262f46", 
                    "type" : "field"
                }
            }, 
            {

            }, 
            {

            }
        ]

As Array of Object of Object

now i have dao class someObject to store the inner most object

public class SomeObject{

private ObjectId itemId;
    private String type;
    public ObjectId getItemId() {
        return itemId;
    }
    public void setItemId(ObjectId itemId) {
        this.itemId = itemId;
    }
    public String getType() {
        return type;
    }
    public void setType(String type) {
        this.type = type;
    }

}

here is the section Dao class

public class Section{

private List<List<SomeObject>> columns;

    public List<List<someObject>> getColumns() {
        return columns;
    }

    public void setColumns(List<List<SomeObject>> columns) {
        this.columns = columns;
    }
}

how section class ever this works fine for first format as i have taken type as List

but breaks for second format as it has diffeferent

i also tried using this class

Public class Section {

private List<Object> columns;

}

this mapps the second format but breaks for first format i got the following error

 "exceptionDetails": "Cannot convert [Document{{itemId=5877f2345449aef957e1d8ec, type=field}}] of type class java.util.ArrayList into an instance of class java.lang.Object! Implement a custom Converter<class java.util.ArrayList, class java.lang.Object> and register it with the CustomConversions.

please can anyone suggest how do i create by dao class such that it can map both the formats ?? do i need to implement custom mapper ? if yes then how ?

Upvotes: 0

Views: 691

Answers (1)

Anar Sultanov
Anar Sultanov

Reputation: 3406

It seems to me you are a little do not understand what is DAO class. What you have are JavaBeans.

And about your question, as you noticed and as reported in the exception details your document is an ArrayList, why not just read it as an ArrayList and then set it to Section field? But if this option does not suit you, you can implement Custom Converter, this is also reported to you in the exception details.

Upvotes: 1

Related Questions