简体   繁体   English

Spring jdbc 插入语句与 sequence.nexval

[英]Spring jdbc insert statement with sequence.nexval

I am using Spring and Postgres, and am trying to do an INSERT.我正在使用 Spring 和 Postgres,并且正在尝试进行 INSERT。 The table approvalsubmission has a sequence called approvalsubmission_ids .approvalsubmission有一个称为approvalsubmission_ids的序列。

So I try use nextval to populate the id column.所以我尝试使用nextval来填充id列。 But I get an error.但我得到一个错误。

Question问题

How do I need to construct the query?我需要如何构建查询?

I have the following query:我有以下查询:

public void updateApprovalSubmission(String corporateTravelRequestUri, String approvalRequestId, byte[] approvalRequestXml, Date requestedDate) {
    String query = "INSERT INTO approvalsubmission(" +
            "id, approvalrequestid, conclusion, conclusionmessage, modifyable, resubmissionof, requesteddate, requested, conclusiondate, approvalrequestxml, escalationlevel) " +
            "VALUES (approvalsubmission_ids.nextval, :approvalrequestid, :conclusion, :conclusionmessage, :modifyable, :resubmissionof, :requesteddate, :requested, :conclusiondate, :approvalrequestxml, :escalationlevel);";
    MapSqlParameterSource parameters = new MapSqlParameterSource();
    parameters.addValue("approvalrequestid", approvalRequestId);
    parameters.addValue("conclusion", null);
    parameters.addValue("conclusionmessage", null);
    parameters.addValue("modifyable", null);
    parameters.addValue("resubmissionof", null);
    parameters.addValue("requesteddate", requestedDate);
    parameters.addValue("requested", corporateTravelRequestUri);
    parameters.addValue("conclusiondate", null);
    parameters.addValue("approvalrequestxml", approvalRequestXml);
    parameters.addValue("escalationlevel", null);

    int noRowsUpdates = namedParameterJdbcTemplate.update(query, parameters);
    logger.info("updateApprovalSubmission: "+noRowsUpdates+" row updated for corporateTravelRequestUri: "+corporateTravelRequestUri+" and approvalRequestId: "+approvalRequestId);
}

It generates the following error:它会产生以下错误:

org.postgresql.util.PSQLException: ERROR: missing FROM-clause entry for table "approvalsubmission_ids" org.postgresql.util.PSQLException:错误:缺少表“approvalsubmission_ids”的 FROM 子句条目

Another choice would be create those id columns using the PostgreSQL type serial ( or subtypes: smallserial , bigserial ).另一种选择是使用 PostgreSQL 类型serial或子类型: smallserialbigserial )创建这些id列。

https://www.postgresql.org/docs/9.5/datatype-numeric.html#DATATYPE-SERIAL https://www.postgresql.org/docs/9.5/datatype-numeric.html#DATATYPE-SERIAL

In that way, you won't need to specify in your insert statement neither the column id nor its "related value", because database will manage that for you.这样,您无需在insert语句中指定列id及其“相关值”,因为数据库将为您管理。

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

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