Karthikeyan
Karthikeyan

Reputation: 526

Object List of Array mapping to List of Pojo

I have a JDBC Result set as List<Object[]> & need to convert it to List<ObjectType> in Spring boot.

// Input Object Array returned by below code
List<Object[]> resultObj = query.getResultList();
// Ex
resultObj[0][0] = new Integer(10);
resultObj[0][1] = "Test";
resultObj[0][2] = "Hello";

// Need map resultObj to List<ObjectType> below type

class ObjectType {
    //maps to Object[0]
    private Integer x;
    //maps to Object[1]
    private String y;
    //maps to Object[2]
    private String z;
}

Since object[] has over 18 elements, instead of mapping manually each index to field of POJO, i tried using Dozer.

<mapping>
        <class-a>java.lang.Object[]</class-a>
        <class-b>com.ObjectType</class-b>
        <field>
            <a>this[0]</a>
            <b>x</b>
        </field>
        <field>
            <a>this[1]</a>
            <b>y</b>
        </field>
</mapping>

I tried following this answer https://stackoverflow.com/a/26556413/350705, but output mapped object has below value - same value for all fields & address space as value

"x": "[Ljava.lang.Object;@2d716a1c",
"y": "[Ljava.lang.Object;@2d716a1c",
"z": "[Ljava.lang.Object;@2d716a1c",

Please suggest if there is any other configurable approach for this.

Upvotes: 1

Views: 1270

Answers (1)

NKR
NKR

Reputation: 186

Try something below that might help. For explanation please refer this link https://sourceforge.net/p/dozer/discussion/452530/thread/012023da/

<mapping>
    <class-a>java.lang.Object[]</class-a>
    <class-b>com.ObjectType</class-b>
    <field>
        <a>this</a>
        <b set-method="add(java.lang.Object)" type="iterate">anything</b>
        <b-hint>your destination object type</b-hint>
    </field>

Upvotes: 0

Related Questions