我的JpaRepository在一个spring引导应用程序中遇到了一个问题,我想在我的数据库上执行一个简单的更新查询,但是原来的原生查询非常烦人,请帮助。
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);
}上面的代码返回以下错误消息
com.microsoft.sqlserver.jdbc.SQLServerException: Incorrect syntax near '@P0'.我曾尝试将SQL方言分别转换为org.hibernate.dialect.SQLServer2008Dialect org.hibernate.dialect.SQLServer2012Dialect,但它们都不起作用。
我还试图以一种不同的方式编写查询,这不会给我带来错误,但它不会更新字段。它可以从方法中检测整数值,但是它将字符串值设置为emply值:
@Query(value = "UPDATE image SET physical_path =(0), status = (1) WHERE Id = (2)", nativeQuery = true)如果有人遇到过同样的问题,请支持
发布于 2020-10-04 19:03:59
将查询设置如下:
@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);位置参数的 :
@Query(value = "UPDATE image i SET path = ?1, status = ?2 WHERE i.Id = ?3", nativeQuery = true)
void update(String path , String status, int Id);发布于 2020-10-04 19:03:29
从春季数据JPA -参考中可以看到参数(以您想要使用的方式)的定义类似于-> ?1, ?2 etc..
另外,请记住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);https://stackoverflow.com/questions/64198541
复制相似问题