In my project, I have created a view that contains multiple joins. The following is part of the query from the view.
a.id,
b.object1,
b.object2,
b.object3,
case when (c.type = 'qrt' then c.object4 else b.object4) end object4,
case when(c.type = 'qrt' then c.object5 else b.object5) end object5
from A a
left join B b on b.a_id = a.id
left join c c on c.b_id = b.id
This query will return the data as follows:
id object1 object2 object3 object4 object5
1 a1 b1 c1 d1 e1
1 a1 b1 c1 d2 e2
2 a2 b2 c2 d3 e3
2 a2 b2 c2 d4 d5
I want to map the result with the following POJO class
public class Example{
private Integer id;
private String object1;
private String object2;
private String object3;
List<InnerExample> innerExample;
}
public class InnerExample{
private String object4;
private String object5;
}
In UI data will be displayed in a table with pagination like below.
table, th, td {
border: 1px solid black;
border-collapse: collapse;
}
<table >
<tr>
<td rowspan = 2>id</td>
<td rowspan = 2>name</td>
<td rowspan = 2>object1</td>
<td rowspan = 2>object2</td>
<td rowspan = 2>object3</td>
<td>object4</td>
<td>object5</td>
</tr>
<tr>
<td>object4</td>
<td>object5</td>
</tr>
<tr>
<td rowspan = 2>id</td>
<td rowspan = 2>name</td>
<td rowspan = 2>object1</td>
<td rowspan = 2>object2</td>
<td rowspan = 2>object3</td>
<td>object4</td>
<td>object5</td>
</tr>
<tr>
<td>object4</td>
<td>object5</td>
</tr>
</table>
I am using Spring JPA and I didn't find an approach to directly map the result to the Example POJO.
Can anyone please help me...
If you are using Spring boot JPA you can directly convert your query results into a class from any of the repository classes that you have written.
Also, add the following constructors in the Example and InnerExample class.
Please keep in mind in here I have used List instead of InnerExample as I am quite unsure how the query is working and is giving list output. But you can modify accordingly.