deepak mahapatra
deepak mahapatra

Reputation: 315

Spring JPA writing a native query with parameters inside single quotes

@Repository
public class PostgresRepository {

@PersistenceContext
EntityManager entityManager;

@Autowired
private JdbcTemplate jdbcTemplate;

public void updatePassword(PostgresDto postgresDto) {

    Query result = entityManager.createNativeQuery("ALTER ROLE :username PASSWORD :password ")
            .setParameter(1,postgresDto.getUsername())
            .setParameter(2,postgresDto.getPassword());
    int results = result.executeUpdate();

    }
}

I am trying to have spring JPA change the role password of a postgres database through the native query. Having issues in setting the parameters of the native query. The postgres statement for altering a role are as follows ALTER ROLE username PASSWORD 'password'.

ERROR :

org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:139)
... 138 common frames omitted
 Caused by: org.postgresql.util.PSQLException: ERROR: syntax error at or near "$1" Position: 12 

Upvotes: 0

Views: 986

Answers (1)

Dmitry Chuiko
Dmitry Chuiko

Reputation: 131

With keyword missing in query. It should be:

ALTER ROLE :username WITH PASSWORD :password

Upvotes: 1

Related Questions