5

Actually we are using fetchInto() to convert the results to list of object.

For example:

Employee pojo matching database table is employee.

List<Employee> employeeList = sql.select(Tables.Employee)
                                 .from(Tables.EMPLOYEE).fetchInto(Employee.class);

similarly, how could we convert the records we are fetching using joins?

For example:

Customer pojo matching database table is customer.

Employee pojo matching database table is employee.

sql.select(<<IWantAllFields>>).from(Tables.CUSTOMER)
                              .join(Tables.EMPLOYEE)
                              .on(Tables.EMPLOYEE.ID.equal(Tables.CUSTOMER.EMPLOYEE_ID))
                              .fetchInto(?);
Syed Shahul
  • 481
  • 4
  • 16

1 Answers1

3

To select all fields from a joined table source, simply select "none":

Result<Record> result =
sql.select().from(Tables.CUSTOMER)
            .join(Tables.EMPLOYEE)
            .on(...)
            .fetch();

jOOQ will then introspect the known table source and generate all column references for you. One way to create a POJO relationship is to use one of the various Result.intoGroups() methods. E.g.:

Map<Integer, List<Customer>> map =
result.intoGroups(CUSTOMER.EMPLOYEE_ID, Customer.class);

This will produce a map of List<Customer> pojos per EMPLOYEE_ID value.

On a side-note: As with any mapping operation that calls upon the DefaultRecordMapper, mapping might not work as expected when your JOIN operation produces two times the same column name (e.g. CUSTOMER.ID and EMPLOYEE.ID) - as the DefaultRecordMapper doesn't know what table a particular column originates from.

For more sophisticated mapping, you should probably implement your own RecordMapperProvider

Lukas Eder
  • 211,314
  • 129
  • 689
  • 1,509
  • Actually I want fields from both the tables. – Syed Shahul Aug 30 '13 at 09:04
  • And I think ModelMapper support this feature, but I am trying to convert each record to custom object it failed. Any help? http://modelmapper.org/user-manual/jooq-integration/ – Syed Shahul Aug 30 '13 at 09:06
  • I'm afraid that jOOQ currently doesn't support one-to-many mapping of results to POJOs. The above hints are the best you can do so far... – Lukas Eder Aug 30 '13 at 10:48
  • 1
    Thanks, now I am trying to implement custom mapping using RecordMapperProvider. – Syed Shahul Sep 05 '13 at 13:08
  • Great. Let me know how it goes. If any questions arise, related to `RecordMapperProvider`, just ask a new SO question, or ask for help on the [jOOQ User Group](https://groups.google.com/forum/#!forum/jooq-user) – Lukas Eder Sep 06 '13 at 06:01
  • I'm just learning JOOQ, but I believe the result.intoGroups(Field, Class) will fail for POJOs if the join result set has multiple columns with the same name. So in this case, your customer name might get the value for the employee name or vice versa with no exceptions thrown. It seems result.intoGroups(Table) is MUCH safer but please correct me if I'm wrong as I've only been using JOOQ for 30 minutes or so :) – nogridbag Jan 19 '15 at 19:14