gpt4 book ai didi

java - Spring JPA 使用单引号内的参数编写 native 查询

转载 作者:行者123 更新时间:2023-12-02 10:57:05 30 4
gpt4 key购买 nike

@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 通过 native 查询更改 postgres 数据库的角色密码。设置 native 查询的参数时遇到问题。用于更改角色的 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

最佳答案

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

ALTER ROLE :username WITH PASSWORD :password

关于java - Spring JPA 使用单引号内的参数编写 native 查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51637758/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com