是否存在布尔值通配符之类的东西?

Is there such a thing as a wildcard for boolean value?

我有一个带有一些自定义查询的 JPA 存储库。我的任务是实现一项需要大量新查询的功能。可以大大减少它们数量的一件事是,如果我可以使用通配符作为布尔值。

我尝试修改查询以使用 'LIKE %',但 Spring 数据不喜欢它,抛出 numberFormatException(无限的 NaN)。

原查询:

 @Query("SELECT SUM(paymentSummary.paidAmount) FROM InvoiceLineItem invoice INNER JOIN PaymentSummary paymentSummary"
    + " ON invoice.invoiceKey=paymentSummary.invoiceKey"
    + " WHERE invoice.balanced=:balanced")
BigDecimal getInvoicePaidTotalByBalanced(@Param("balanced") boolean balanced);

理想情况下,我想使用相同的查询,但不是传递 true 或 false,而是传递一个通配符,以便它对所有记录起作用,而不管 true/false 状态如何。

截至目前,我知道如何执行此操作的唯一方法是创建第二个查询,与第一个查询相同,但减去 where 子句。

改用 IN 运算符怎么样? (TRUE, FALSE) 将成为您的通配符。

或者,您应该可以使用 WHERE :balanced IS NULL OR invoice.balanced=:balanced。在这种情况下,将 boolean 更改为 Boolean 并且 null 成为通配符。