Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/370.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java SpringJPA使用单引号中的参数编写本机查询_Java_Spring_Postgresql_Jpa - Fatal编程技术网

Java SpringJPA使用单引号中的参数编写本机查询

Java SpringJPA使用单引号中的参数编写本机查询,java,spring,postgresql,jpa,Java,Spring,Postgresql,Jpa,我试图让SpringJPA通过本机查询更改postgres数据库的角色密码。在设置本机查询的参数时遇到问题。更改角色的postgres语句如下所示ALTER role username PASSWORD'PASSWORD' 错误: @Repository public class PostgresRepository { @PersistenceContext EntityManager entityManager; @Autowired private JdbcTemplate jdbcT

我试图让SpringJPA通过本机查询更改postgres数据库的角色密码。在设置本机查询的参数时遇到问题。更改角色的postgres语句如下所示
ALTER role username PASSWORD'PASSWORD'

错误:

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

    }
}

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

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 

这不能作为本机查询使用,因为ALTERROLE语句类似于密码为“hu8jmn3”的ALTERROLE davide;使用JPA本机查询从参数中解析出单引号
ALTER ROLE :username WITH PASSWORD :password