[英]Map fetch/result from jooq to specific Record
When I currently query with Jooq I am explicitly casting each record-object to the expected record-type. 当我当前使用Jooq查询时,我明确地将每个记录对象转换为预期的记录类型。
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, with Jooq, possibly to cast the result straight into the desired record-type? 与Jooq一起,是否可能将结果直接转换为所需的记录类型?
Such as (this does not compile): 如(这不编译):
Result<CountryRecord> countryRecords = (Result<CountryRecord>) sql.select().from(Tables.COUNTRY).fetch();
for (CountryRecord cr : countryRecords) {
cr.getNamet();
//etc...
}
@Lukas, @Lukas,
Actually we are using fetchInto()
to convert the results to list of object. 实际上我们使用
fetchInto()
将结果转换为对象列表。
For example: 例如:
Employee
pojo matching database table is employee. Employee
pojo匹配数据库表是员工。
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
. Customer
pojo匹配数据库表是customer
。
Employee
pojo matching database table is employee
. Employee
pojo匹配数据库表是employee
。
sql.select(<<IWantAllFields>>).from(Tables.CUSTOMER)
.join(Tables.EMPLOYEE)
.on(Tables.EMPLOYEE.ID.equal(Tables.CUSTOMER.EMPLOYEE_ID))
.fetchInto(?);
You shouldn't be using the select().from(...)
syntax when you want to fetch generated record types. 当您想要获取生成的记录类型时,不应该使用
select().from(...)
语法。 Use selectFrom()
instead. 请改用
selectFrom()
。 This is documented here: 这在此处记录:
http://www.jooq.org/doc/3.1/manual/sql-execution/fetching/record-vs-tablerecord http://www.jooq.org/doc/3.1/manual/sql-execution/fetching/record-vs-tablerecord
So your query should be: 所以你的查询应该是:
Result<CountryRecord> countryRecords = sql.selectFrom(Tables.COUNTRY).fetch();
for (CountryRecord cr : countryRecords) {
cr.getNamet();
//etc...
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.