I have two tables in my use case..
- Project
- Task
A project can have multiple task associated with that project.
In task table I have FK relationship with project table, simple M:M relationship.
As in R2dbcRepository we do not have JPA support for table relationship , I am using join query with R2DBC DatabaseClient and executing the SQL query to get the data.
My SQL Query like this : select project.\"projectName\",project.\"projectId\",tsk.\"taskName\" \n" + "from evolutionlinkgroup.task as tsk \n" + "inner join evolutionlinkgroup.project on project.\"projectId\"=tsk.\"taskProjectId\"
After exciting this query I am mapping this into object :
client.sql(queryForAllProj) .map(mapper::apply) .all();
My Mapper Function :
public Project apply(Row row, Object o) {
Integer taskId = row.get("taskId", Integer.class);
String taskName = row.get("taskName", String.class);
Integer projectId = row.get("projectId", Integer.class);
String projectName = row.get("projectName", String.class);
Task task = new Task(taskId,taskName,null);
Project project = new Project(projectId, projectName,Arrays.asList(task));
task.setProjectId(new Project(projectId, projectName,null));
return project;
}
Which return me back Flux of project object : Flux<Project>
the mapper return me back data in this way, the same way my SQL fetch data from DB :
Project ID | Project Name | Task ID
001 | Project1 | TSK001
001 | Project1 | TSK002
001 | Project1 | TSK003
002 | Project2 | TSK004
002 | Project2 | TSK006
003 | Project3 | TSK001
now from this Flux of project I want to convert it something like this.
Project ID | Project Name | Task ID
001 | Project1 | ListOfTask(TSK001,TSK002,TSK003)
002 | Project2 | ListOfTask(TSK004,TSK006)
003 | Project3 | ListOfTask(TSK001)
Can any one suggest me how can I do that by doing something in the mapper function or after I receive the response from mapper again run some transformation on top of that data.
I tried in this way :
result.collect(
Collectors.toMap(Project::getProjectId, Function.identity(), (Project project1, Project project2) -> {
project1.getTaskList().addAll(project2.getTaskList());
project2.getTaskList().clear();
return project1;
})
But this is not what I am looking for. It returning me back map<key,project> , but I am not able convert that into Flux of project object along with the list of task object which are associated with that project.