gpt4 book ai didi

java - Querydsl 多次加入同一张表

转载 作者:塔克拉玛干 更新时间:2023-11-03 03:06:09 25 4
gpt4 key购买 nike

假设我有两个表TaskCompanyCompany 包含列 idnameTask 有两列 customerIdproviderId,它们链接回 Companyid 列>.

使用 Querydsl 我如何加入 Company 表两次,以便我可以获得 customerId 指定的每个公司的 name code>providerId?

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

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

我不知道如何为我要加入的表设置别名,以便区分客户名称和提供商名称。我尝试执行 new QCompany("company as provider") 但这没有用。任何人都知道如何做到这一点?

最佳答案

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

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

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

关于java - Querydsl 多次加入同一张表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28331489/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com