0

I've got a List<Object[]> as the result of a select query, using NativeQuery and Java 8. Then I need to create to convert this plain list into a nested object list. This would be an example:

--List from select--

{ 
"result":[
    {
    "name": "Peter",
    "age": "42",
    "studentName": "Mark",
    "studentAge": "21"
    },
     {
    "name": "Peter",
    "age": "42",
    "studentName": "Sarah",
    "studentAge": "23"
    },
    {
    "name": "Ellie",
    "age": "37",
    "studentName": "Michel",
    "studentAge": "21"
    }
    ]
}

--Desired output--

{ 
"result":[{
    "name": "Peter",
    "age": "42",
    "students": [
       {
       "name": "Peter",
       "age": "21"},
       {
       "name": "Sarah",
       "age": "23"}
        ]
        },
     {
    "name": "Ellie",
    "age": "37",
    "students": [
       {
       "name": "Michael",
       "age": "21"}
        ]
        }
]
}

How can this be done in Java 8 or Native Query?

Thank you

Akshay
  • 158
  • 1
  • 7
Rachel
  • 65
  • 11
  • 1
    What have you tried so far and what did not work for you? – Ghokun Jul 14 '21 at 09:50
  • If the end result you want is a java object (Or a list of specific java objects), why do you not use hibernate as the object-relation mapping tool it is intended instead of making a native query and getting an array of objects? – OH GOD SPIDERS Jul 14 '21 at 09:53
  • Thats correct I didn't realize that I should be using Hibernate to do the relational mapping instead of NativeQuery, that would be the best approach. Thank you! – Rachel Jul 14 '21 at 10:13

0 Answers0