I'm trying to construct a query against multiple tables with a one to many relationship.
Thew model I'm trying to return is:
{
'dept_id' : 123456,
'dept_name' : 'department 1',
'employee_ids' : [9, 10, 11, 12, 13]
}
My initial query is gets the data of the parent object:
public Uni<Department> getDepartmentById(Long deptId) {
return client.preparedQuery("SELECT * from department where department_id = ?1")
.execute(Tuple.of(deptId))
.onItem().apply(RowSet::iterator)
.onItem().apply(iterator -> iterator.hasNext() ? from(iterator.next()) : null);
}
Though somehow, I need to add in the sub-query for the child ids, something like:
return client.preparedQuery("SELECT * from department where department_id = ?1")
.execute(Tuple.of(deptId))
.onItem().apply(RowSet::iterator)
.onItem().apply(iterator -> iterator.hasNext() ? from(iterator.next()) : null)
.onItem().apply(dept -> {
client.preparedQuery("select emp_id from employee where dept_id = ?1")
.execute(Tuple.of(dept.getDeptId())
// for each returned append to an array then add to the department
// dept.setEmployeeIds(????)
});
and then collect the employee_ids into single array to add to the department.
Do I use some Zip approach where I create the employee RowSet first then get the department or is there another transform I need to use to make this work?