1

I have a simple sql query which increments version by 1 and returns the current value in postgresql such as:

UPDATE table SET version = version + 1 where id = 'XXX' RETURNING version

However I am unable to use it in jdbcTemplate.update statement:

int version = jdbcTemplate.update(sqlString, new Object[] {id}); 

throws exception:

A result was returned when none was expected

org.springframework.web.util.NestedServletException: Request processing failed; nested exception is org.springframework.dao.DataIntegrityViolationException: PreparedStatementCallback; SQL [UPDATE table SET version = version + 1 where id = ? RETURNING version]; A result was returned when none was expected.; nested exception is org.postgresql.util.PSQLException: A result was returned when none was expected.

What is the correct way to use RETURNING statement?

3
  • what exception it throw! Commented Jun 5, 2018 at 8:25
  • org.springframework.web.util.NestedServletException: Request processing failed; nested exception is org.springframework.dao.DataIntegrityViolationException: PreparedStatementCallback; SQL [UPDATE table SET version = version + 1 where id = ? RETURNING version]; A result was returned when none was expected.; nested exception is org.postgresql.util.PSQLException: A result was returned when none was expected. Commented Jun 5, 2018 at 8:26
  • Please post a minimal reproducible example Commented Jun 5, 2018 at 10:43

1 Answer 1

1

I have found the solution. Instead of jdbcTemplate.update I used jdbcTemplate.query and cast number to int to obtain the value of version. Thank you all.

    Number number = jdbcTemplate.queryForObject(sqlString, new Object[] {id}, Integer.class);
    int version;

    version = number != null ? number.intValue() : 0;
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.