HSQLDB org.springframework.dao.TransientDataAccessResourceException 复杂 select

HSQLDB org.springframework.dao.TransientDataAccessResourceException on complex select

我有一个查询 运行 在 Oracle 中很好,但是当我尝试 运行 在 HSQLDB 中(使用 Oracle 兼容模式)相同的查询时收到 org.springframework.dao.TransientDataAccessResourceException

这里是有问题的查询:

select 
    Orders.id,
    (select sum(decode(Orders.status, 'C', Orderlines.qty, 0)) 
     from Orderlines where orderId = Orders.id
    ) as "productQuantity"
from Orders
join Orderlines on Orders.id = Orderlines.orderId
where Orders.customerId = ?
group by Orders.id, Orders.status

这是堆栈跟踪的根

Caused by: org.hsqldb.HsqlException: java.lang.NullPointerException
at org.hsqldb.error.Error.error(Unknown Source)
at org.hsqldb.result.Result.newErrorResult(Unknown Source)
at org.hsqldb.result.Result.newErrorResult(Unknown Source)
at org.hsqldb.StatementDMQL.execute(Unknown Source)
at org.hsqldb.Session.executeCompiledStatement(Unknown Source)
at org.hsqldb.Session.execute(Unknown Source)
... 60 more
Caused by: java.lang.NullPointerException
at org.hsqldb.ExpressionColumn.getValue(Unknown Source)
at org.hsqldb.Expression.getValue(Unknown Source)
at org.hsqldb.ExpressionOp.getValue(Unknown Source)
at org.hsqldb.ExpressionAggregate.updateAggregatingValue(Unknown Source)
at org.hsqldb.QuerySpecification.buildResult(Unknown Source)
at org.hsqldb.QuerySpecification.getSingleResult(Unknown Source)
at org.hsqldb.QuerySpecification.getResult(Unknown Source)
at org.hsqldb.StatementQuery.getResult(Unknown Source)
... 63 more

如果我通过删除 decode 子句稍微简化查询,HSQLDB 可以处理它。

select 
    Orders.id,
    (select sum(Orders.status) 
     from Orderlines where orderId = Orders.id
    ) as "productQuantity"
from Orders
join Orderlines on Orders.id = Orderlines.orderId
where Orders.customerId = ?
group by Orders.id, Orders.status

这是 HSQLDB 的已知限制吗?

有什么办法可以解决这个问题吗?我正在尝试使用 HSQLDB 进行单元测试,因此修改查询并不是一个真正的选择,除非该解决方案也适用于 Oracle。

这是 HSQLDB 的限制。您可以试试 CASE Order.status WHEN 'C' THEN Orderlines.qty ELSE 0 END,看看它是否有效。