I have been playing with some of the new features of JOOQ 3.17 such as type safe nested table records mixed with implicit joins as is described here:
https://blog.jooq.org/projecting-type-safe-nested-tablerecords-with-jooq-3-17/
We have a complex view where you can modify many properties of a "company" object. Our old code had a zillion hibernate methods to CRUD related records for the company object on one rather large UI. Now I want to rewrite this in JOOQ. I came up with a single query that pulls a CompanyRecord and related records like this:
Record4<CompanyRecord, Result<ServiceCompanyPreferenceRecord>, Result<SubsidiaryRecord>, Result<CompanyCo2ParameterRecord>> fancyTypeResult =
dslContext.get().select(
Tables.COMPANY,
multiset(
selectFrom(Tables.SERVICE_COMPANY_PREFERENCE)
.where(Tables.SERVICE_COMPANY_PREFERENCE.COMPANY_ID.eq(Tables.COMPANY.ID))
),
multiset(
selectFrom(Tables.SUBSIDIARY)
.where(Tables.SUBSIDIARY.COMPANY_ID.eq(Tables.COMPANY.ID))
),
multiset(
selectFrom(Tables.COMPANY_CO2_PARAMETER)
.where(Tables.COMPANY_CO2_PARAMETER.COMPANY_ID.eq(Tables.COMPANY.ID))
)
)
.from(Tables.COMPANY)
.where(Tables.COMPANY.ID.eq(companyId))
.fetchOne();
This is fantastic because I can modify and save the CompanyRecord or and related ServiceCompanyPreferenceRecord, SubsidiaryRecord,or CompanyCo2ParameterRecord and those rows in the db are updated.
One problem I am having is passing type "Record4<CompanyRecord, Result<ServiceCompanyPreferenceRecord>, Result<SubsidiaryRecord>, Result<CompanyCo2ParameterRecord>>" is rather verbose. So having a function that finds a company all all records like this
public Record4<CompanyRecord, Result<ServiceCompanyPreferenceRecord>, Result<SubsidiaryRecord>, Result<CompanyCo2ParameterRecord>> loadCompanyAndRelatedPreferences(Long companyId){ ....
Could be a bit akward. I am wondering if simply making a POJO that holds a field of type "Record4<CompanyRecord, Result<ServiceCompanyPreferenceRecord>, Result<SubsidiaryRecord>, Result<CompanyCo2ParameterRecord>>" would make it easier as I can use that POJO without re-writing that type over and over. Another thought would be is this a good use case for Java records?
We have a number of screens that follow this pattern. Pull a record and related records, CRUD them. Any ideas on a nice way to handle this?