Querydsl 多次加入相同的 table

Querydsl join on same table multiple times

假设我有两个 tables TaskCompanyCompany 有列 idnameTask 有两列 customerIdproviderId,其中 link 返回 Companyid 列。

使用 Querydsl 如何加入 Company table 两次,以便我可以为 customerId 和 [=21= 指定的每个公司获取 name ]?

可能更好地解释我正在尝试的代码:

Configuration configuration = new Configuration(templates);
JPASQLQuery query = new JPASQLQuery(this.entityManager, configuration);

QTask task = QTask.task;
QCompany customer = QCompany.company;
QCompany provider = QCompany.company;

JPASQLQuery sql = query.from(task).join(customer).on(customer.id.eq(task.customerId))
    .join(provider).on(provider.id.eq(task.providerId));

return sql.list(task.id, customer.name.as("customerName"), provider.name.as("providerName"));

生成 SQL:

select task.id, company.name as customerName, company.name as providerName from task join company on company.id = task.customerId

我真的很希望它是:

select task.id, customer.name as customerName, provider.name as providerName from task join company as customer on customer.id = task.customerId join company as provider on provider.id = task.providerId

我不知道如何为我加入的 table 起别名,这样我才能区分客户名称和提供商名称。我试过 new QCompany("company as provider") 但没有用。有人知道怎么做吗?

如果您需要变量,只需执行以下操作

QCompany customer = new QCompany("customer");
QCompany provider = new QCompany("provider");

重新分配默认变量 QCompany.company 没有帮助