简体   繁体   English

JOOQ SQL给出1个参数但预期为0

[英]JOOQ SQL given 1 parameters but expected 0

I'm trying to figure out how to use JOOQ with NamedParameterJdbcTemplate. 我试图找出如何使用NamedParameterJdbcTemplate JOOQ。 I have successfully created other queries, but I'm stuck at creating a query containing the WHERE clause. 我已经成功创建了其他查询,但我坚持创建一个包含WHERE子句的查询。 When I try to run the method below, I get the error org.springframework.dao.InvalidDataAccessApiUsageException: SQL [select "first_name" from "customer" where "id" = cast(? as integer)]: given 1 parameters but expected 0 当我尝试运行下面的方法时,我得到错误org.springframework.dao.InvalidDataAccessApiUsageException:SQL [select“first_name”from“customer”,其中“id”= cast(?as integer)]:给定1个参数但预期为0

I get the same error when I try to use only integers in equals(), like... .where(fieldByName("id").equal(1001), same error and .where(fieldByName("id").equal(id), same error. 当我尝试仅使用equals()中的整数时,我得到相同的错误,例如...... .where(fieldByName(“id”)。equal(1001),相同的错误和.where(fieldByName(“id”)。 (id),同样的错误。

If I remove the WHERE clause, the query itself seems to work fine. 如果我删除WHERE子句,查询本身似乎工作正常。

What am I doing wrong here? 我在这做错了什么? It seems to me that the SQL syntax is correct. 在我看来,SQL语法是正确的。 It's probably me being stupid, but I really can't find what's wrong here. 这可能是我的愚蠢,但我真的找不到这里的错。 Please help! 请帮忙!

public String getCustomerFirstName(int id) {
    Query query = create.select(fieldByName("first_name"))
                    .from(tableByName("customer"))
                    .where(fieldByName("id").equal(param("id", id)));
    Param param = query.getParam("id");
    SqlParameterSource namedParameters = new MapSqlParameterSource(param.getName(), id);
    return this.getNamedParameterJdbcTemplate().queryForObject(query.getSQL(), namedParameters, String.class);
}

In order to have jOOQ generate named parameters, you have to explicitly tell it to do so. 为了让jOOQ生成命名参数,您必须明确告诉它这样做。

In your case, this would make 在你的情况下,这将是

String sql = create.renderNamedParams(query);

The above would replace your call to 以上将取代你的电话

query.getSQL();

Note, this is documented here: 请注意,这在此处记录:

http://www.jooq.org/doc/3.0/manual/sql-building/bind-values/named-parameters http://www.jooq.org/doc/3.0/manual/sql-building/bind-values/named-parameters

A future version of jOOQ (probably 3.1) will probably support initialising your DSLContext with a setting to always render named parameters as such. 未来版本的jOOQ(可能是3.1)可能会支持初始化您的DSLContext ,并设置为始终呈现命名参数。 This is registered as a feature request on the roadmap: 这是在路线图上注册为功能请求:

https://github.com/jOOQ/jOOQ/issues/2414 https://github.com/jOOQ/jOOQ/issues/2414

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM