Hey Lukas!

I'm following up on my stackOverflow comment on 
https://stackoverflow.com/questions/53122656/mapping-jooq-join-result-into-multiple-pojos#comment94047335_53122656.

When stepping away from JPA and using something like jooq, I miss the way 
that JPA provides you with a object graph.

Let's say we have the tables `university`, `department` and `employee` and 
we want to join them, then get a Java representation something like:

public class University {
    private final Long id;
    private final List<Department> departments;
    // some other fields
}


pubic class Department {
    private final Long id;
    private final Long universityId;
    private final List<Employee> employees;
    // some other fields
}


public class Employee {
    private final Long id;
    private final Long departmentId;
    // some other fields
}


>From jooq, we get the denormalised list of pojos that represent a row. When 
putting it into the desired structure, the best I've come up with is 
overriding the `equals` method to compare `id` and using Java 8 streams.

E.G. (assume a builder for each class)

List<University> universities = rows
                                .stream()
                                .map(p -> University
                                         .builder()
                                         .id(p.getUniverisityId())
                                          // some other fields
                                         .build())
                                .distinct()
                                .collect(Collectors.toList());


List<Department> departments = rows
                               .stream()
                               .map(p -> Department
                                        .builder()
                                        .id(p.getDepartmentId())
                                         // some other fields
                                        .build())
                               .distinct()
                               .collect(Collectors.toList());


Map<Long, List<Department>> universityDepartment = departments
                                                  .stream()
                                                  .collect(Collectors.
groupingBy(Department::getUniversityId));


universities = //loop through universities, set the departments based on 
universityDepartment 
map (I'm using lombok so would stream and map again using `toBuilder()`)


I feel like I'm missing a trick. Is there a better way?

-- 
You received this message because you are subscribed to the Google Groups "jOOQ 
User Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/d/optout.

Reply via email to