简体   繁体   English

JpaRepository 不读取本机查询中的字符串参数

[英]JpaRepository does not read string parameter in a native query

I am facing a problem with my JpaRepository in a spring boot application I want to perform a simple update query on my database, but it turns out that the native query is quite annoying, please help我在 Spring Boot 应用程序中遇到了 JpaRepository 的问题我想对我的数据库执行一个简单的更新查询,但结果是原生查询很烦人,请帮忙

public interface ImageRepository extends JpaRepository<Image, Integer> {
    @Modifying 
    @Transactional
    @Query(value = "UPDATE image SET path =(0?), status = (1?) WHERE Id = (2?)", nativeQuery = true)
    void update(String path ,String status,int Id);

}

the code above returns the following error message上面的代码返回以下错误信息

com.microsoft.sqlserver.jdbc.SQLServerException: Incorrect syntax near '@P0'.

I have tried to change SQL dialect to org.hibernate.dialect.SQLServer2008Dialect org.hibernate.dialect.SQLServer2012Dialect respectively and non of them worked.我试图分别将 SQL 方言更改为 org.hibernate.dialect.SQLServer2008Dialect org.hibernate.dialect.SQLServer2012Dialect 并且它们都没有工作。

I tried also to write the query in a different way which does not give me an error but it does not update the fields.我还尝试以不同的方式编写查询,这不会给我一个错误,但它不会更新字段。 it can detect the integer value from the method but it will set the string values to an emply value:它可以从方法中检测整数值,但会将字符串值设置为 emply 值:

    @Query(value = "UPDATE image SET physical_path =(0), status = (1) WHERE Id = (2)", nativeQuery = true)

If anyone has faced the same issue please support如果有人遇到同样的问题,请支持

From the Spring Data JPA - Reference you can see that the parameters (in the way you want to use them) are defined like -> ?1, ?2 etc..Spring Data JPA - Reference您可以看到参数(以您想要使用的方式)定义为 -> ?1, ?2 etc..

Also, please keep in mind that the JPQL syntax is slightly different than plain sql.另外,请记住 JPQL 语法与普通 sql 略有不同。

@Modifying
@Query("update Image i set i.path = ?1 where i.status = ?2 where i.id = ?3")
void update(String path, String status, int id);

Frame the query like this :像这样构建查询:

@Query(value = "UPDATE image i SET path =:path, status = :status WHERE i.Id = :Id", nativeQuery = true)
void update(@Param("path") String path , @Param("status") String status, @Param("Id") int Id);

For positional parameters :对于位置参数:

@Query(value = "UPDATE image i SET path = ?1, status = ?2 WHERE i.Id = ?3", nativeQuery = true)
void update(String path , String status, int Id);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM