How to do an UPDATE with Sqel Expression?

Asked

Viewed 25 times

-1

I’m trying to do an UPDATE and I’m not getting it, someone can identify a point of failure ?

@Query(value="UPDATE cliente SET nome = #{#cliente.nome} WHERE id_cliente = 1", nativeQuery=true)
Cliente updateName(@Param("cliente") Cliente cliente);

The Error Displayed in the Console Is This:

2020-01-24 23:25:17.966 ERROR 14368 --- [nio-8080-exec-2] o.a.c.c.C.[.[.[/].[dispatcherServlet]    : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.dao.InvalidDataAccessResourceUsageException: could not extract ResultSet; SQL [n/a]; nested exception is org.hibernate.exception.SQLGrammarException: could not extract ResultSet] with root cause

org.postgresql.util.PSQLException: ERROR: syntax error at or near "{"
  Posição: 28
  • #cliente.nome? I never saw that syntax, it wouldn’t be :cliente.nome? in addition, you are using the query cliente.nome, but this assigning the parameter as @Param("cliente"), I don’t think it’s gonna work...

  • 2

    @Ricardopunctual this is the syntax used by Spring Expression Language (Spel)

  • humm interesting @nullptr never used so, thanks for sharing

1 answer

1


Following the syntax of documentation, looks like you forgot one : before the parameter:

@Query(value="UPDATE cliente SET nome = :#{#cliente.nome} WHERE id_cliente = 1", nativeQuery=true)
Cliente updateName(@Param("cliente") Cliente cliente);

You will probably also change the value of id_cliente, can be done the same way, otherwise you will only update the ID 1.

It is also important to point out that this syntax is available from the Spring Data JPA 1.4, then if it still doesn’t work check if you are using the version compatible with this functionality.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.