H2数据库:预期" ALL,ANY,SOME,SELECT,FROM,WITH"

时间:2018-05-14 11:28:02

标签: sql h2 spring-jdbc jdbctemplate

我使用H2作为内存数据库,用于单元测试我们的应用程序。此查询为此sql查询提供错误expected "ALL, ANY, SOME, SELECT, FROM, WITH"

SELECT CF.*, ROWNUM CASHFLOW_INDEX
FROM ( SELECT * FROM
TB_CASHFLOWS CFW WHERE CFW.CASHFLOW_DATE >= :runDate AND
CFW.CASHFLOW_TYPE <> 'ISSUE' AND CFW.ISIN = :securityCode ORDER BY
CFW.CASHFLOW_DATE,CFW.CASHFLOW_TYPE ASC ) CF

在runDate位置,如下所示:

SELECT CF.*, ROWNUM CASHFLOW_INDEX
FROM ( SELECT * FROM
TB_CASHFLOWS CFW WHERE CFW.CASHFLOW_DATE >= :[*]RUNDATE AND
CFW.CASHFLOW_TYPE <> 'ISSUE' AND CFW.ISIN = :SECURITYCODE ORDER BY
CFW.CASHFLOW_DATE,CFW.CASHFLOW_TYPE ASC ) CF

但是当参数以&#39; 2017-02-28&#39;给出时,同样的查询在h2控制台中正常工作。和&#39; USD_TREASURY_28FEB&#39;。此查询将由spring jdbc模板执行。这是H2的参数传递方式的问题吗?

java代码如下:

Object[] cashflowQueryArgs = new Object[] {"2017-02-28","USD_TREASURY_28FEB" };

List<Cashflow> instrumentCashflows = getJdbcTemplate().query(
    cashflowsQuery, 
    cashflowQueryArgs,
    new BeanPropertyRowMapper<Cashflow>(Cashflow.class));

1 个答案:

答案 0 :(得分:1)

幸运的是,我找到了解决方案。我认为H2不支持命名参数。所以我将命名参数更改为正常的问号,并且它有效!

SELECT CF.*, ROWNUM CASHFLOW_INDEX
FROM ( SELECT * FROM
TB_CASHFLOWS CFW WHERE CFW.CASHFLOW_DATE >= ? AND
CFW.CASHFLOW_TYPE <> 'ISSUE' AND CFW.ISIN = ? ORDER BY
CFW.CASHFLOW_DATE,CFW.CASHFLOW_TYPE ASC ) CF

针对命名参数进行了更新。

对于Oracle SQL,当将一组参数传递给查询方法时,spring jdbc模板对于normal和named参数都正常工作。但是对于H2,我必须使用命名参数模板和SqlParameterSource。示例如下:

MapSqlParameterSource cashflowQueryParamSource = new MapSqlParameterSource();
cashflowQueryParamSource.addValue("runDate", cashflowQueryArgs[0]);
cashflowQueryParamSource.addValue("securityCode", cashflowQueryArgs[1]);

List<Cashflow> instrumentCashflows = namedJdbcTemplate.query(cashflowsQuery, cashflowQueryParamSource,
                new BeanPropertyRowMapper<Cashflow>(Cashflow.class));

希望这有帮助。