Spring JPA编写带有单引号内参数的本机查询

时间:2018-08-01 16:03:37

标签: java spring postgresql jpa

@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();

    }
}

我正在尝试让Spring JPA通过本机查询更改postgres数据库的角色密码。在设置本机查询的参数时遇到问题。更改角色的postgres语句如下ALTER ROLE username PASSWORD 'password'

错误:

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 

1 个答案:

答案 0 :(得分:1)

查询中缺少关键字。应该是:

ALTER ROLE :username WITH PASSWORD :password