Map display / result from jooq to specific record

When I am currently querying Jooq, I explicitly throw each record object into the expected record type.

Result<Record> result = sql.select().from(Tables.COUNTRY).fetch(); for (Record r : result) { CountryRecord countryRecord = (CountryRecord) r; //Extract data from countryRecord countryRecord.getId(); } 

Is it possible, with Jooq, to ​​bring the result directly to the desired record type?

For example (this does not compile):

 Result<CountryRecord> countryRecords = (Result<CountryRecord>) sql.select().from(Tables.COUNTRY).fetch(); for (CountryRecord cr : countryRecords) { cr.getNamet(); //etc... } 
+4
source share
2 answers

You should not use select().from(...) syntax if you want to get the generated post types. Use selectFrom() . This is described here:

http://www.jooq.org/doc/3.1/manual/sql-execution/fetching/record-vs-tablerecord

So your request should be:

 Result<CountryRecord> countryRecords = sql.selectFrom(Tables.COUNTRY).fetch(); for (CountryRecord cr : countryRecords) { cr.getNamet(); //etc... } 
+5
source

@Lukas,

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(?); 
+5
source

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


All Articles