简体   繁体   中英

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

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.

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:

    @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..

Also, please keep in mind that the JPQL syntax is slightly different than plain 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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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