JOOQ:如何将记录映射到别名 table(来自嵌套查询)

JOOQ: How to map a record to an aliased table (from a nested query)

我有一个带连接的嵌套查询,我正在尝试将结果映射回 table。查询类似于:

var nested = context.select(BUSINESS.fields()).from(BUSINESS).limit(size).offset(size * page);
var idField = nested.field(BUSINESS.UIDBUSINESSID);

var result = context.select(nested.fields()).select(BUSINESSALIAS.fields())
.select(BUSINESSCONTACT.fields()).select(BUSINESSIDENTIFIER.fields())
.select(SITEADDRESS.fields()).select(WEBSITE.fields())
.from(nested)
.leftJoin(BUSINESSALIAS)
.on(idField.eq(BUSINESSALIAS.BUSINESSID))
.leftJoin(BUSINESSCONTACT)
.on(idField.eq(BUSINESSCONTACT.BUSINESSID))
.leftJoin(BUSINESSIDENTIFIER)
.on(idField.eq(BUSINESSIDENTIFIER.BUSINESSID))
.leftJoin(SITEADDRESS)
.on(idField.eq(SITEADDRESS.BUSINESSID))
.leftJoin(WEBSITE)
.on(idField.eq(WEBSITE.BUSINESSID))
.fetch();

像这样 record.into(BUSINESS).into(Business.class) 将结果映射回 BUSINESS 会导致大量异常,因为某些字段的匹配不明确。 实际错误是:

Ambiguous match found for BusinessId. Both "alias_34775958"."BusinessId" and "MCDH"."dbo"."SiteAddress"."BusinessId" match

如何在将记录映射到 table 时告诉 JOOQ BUSINESS table 限定符应该是别名 alias_34775958

您对 record.into(BUSINESS) 的调用导致了这种歧义,因为您的查询实际上没有来自 BUSINESS table 的任何列(尽管您在 table派生 table).

请尝试使用 record.into(nested.fields()),这将生成包括生成的别名在内的准确字段引用。或者,您也可以使用带有显式别名的 table:

var nested = table(select(BUSINESS.fields())
                   .from(BUSINESS)
                   .limit(size)
                   .offset(size * page)).as("b");

现在,nestedTable<?> 类型而不是 Select<?>,您可以直接在 record.into(nested) 调用中使用它。