【发布时间】:2021-08-27 16:12:25
【问题描述】:
我正在尝试使用连接 2 个表进行动态 sql 投影。这里 visibleColumns 是逗号分隔的字符串(我想在 SQL 选择语句中显示的动态列)。
QActive qActive = QActive.active;.
QCustomer qCustomer = QCustomer.customer;
QBaseCounterParty qBaseCounterParty = QBaseCounterParty.baseCounterParty;
StringExpression path = Expressions.stringPath(qActive, visibleColumns);
return JPQLQuery<String> resultSet = query.select(path).from(qActive).innerJoin(qActive.customer, qCustomer).on(qInvoice.customer.id.eq(qCustomer.id))
.innerJoin(qBaseCounterParty).on(qCustomer.id.eq(qBaseCounterParty.id)).where(queryDslSpec).orderBy(sort)
.offset(request.getPagination().getStartRow()).limit(request.getPagination().getRowsPerPage());
所以每当我尝试运行它时,它都会使用 400 Bad Request 创建如下查询:
select active.docNumber, customerName from Active active inner join Customer customer with active.id = customer.id inner join BaseCounterParty baseCounterParty with customer.id = baseCounterParty.id order by active.transactionDate desc
我不确定如何从其他表中选择列,因为列完全是动态的。
【问题讨论】:
-
您无法从字符串中获取列。这不是 QueryDSL 的工作方式。您必须将列与 Q 类型匹配,并将这些 Qtypes 的数组传递给 SELECT。
-
@GuillaumeF。你能告诉我如何用代码做到这一点。
-
@GuillaumeF。我需要选择动态列,但不确定如何匹配 Qtype 中的列,因为它是动态的,所以我不知道哪个列可以出现。
-
.select(qCustomer.name, qBaseCounterParty.whatever, ...)。 fetch 会返回一个 Tuple 对象,然后你可以做tuple.get(qCustomer.name) -
@GuillaumeF。在 select 语句中,我不知道 UI 将发送哪些列,而 UI 实际上发送了一个列列表,所以我如何选择并且表之间也存在连接,所以不知道哪一列来自哪个表以便获取该列的 QType 很难
标签: java spring spring-data-jpa querydsl