Custom Converter for Jooq Result

In fact, we use fetchInto() to convert the results to a list of objects.

For instance:

Employee The pojo mapping database table is an employee.

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

similarly, how can we convert the records we retrieve using joins?

For instance:

Customer Pojo Customer . Compliance database table.

Employee pojo mapping database table.

 sql.select(<<IWantAllFields>>).from(Tables.CUSTOMER) .join(Tables.EMPLOYEE) .on(Tables.EMPLOYEE.ID.equal(Tables.CUSTOMER.EMPLOYEE_ID)) .fetchInto(?); 
+4
source share
1 answer

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

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

jOOQ then examines a known table source and generates all the column references for you. One way to create a POJO relationship is to use one of the Result.intoGroups() methods. For instance:.

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

This will display the List<Customer> pojos value for EMPLOYEE_ID .

On a side note: as with any mapping operation that calls DefaultRecordMapper , mapping may not work as expected when your JOIN produces the same column name twice (for example, CUSTOMER.ID and EMPLOYEE.ID ) - since DefaultRecordMapper Doesn't know from which table a particular column is coming from.

For more complex comparisons, you should probably implement your own RecordMapperProvider

+3
source

Source: https://habr.com/ru/post/1499606/


All Articles