![](/img/trans.png)
[英]Why am I getting a unexpected type, variable required error? even though I am using a variable
[英]why I am getting error as unexpected token
我正在尝试使用此查询更新员工工资。 为什么我收到unexpected token:near line 1 in JPA Query
错误中的unexpected token:near line 1 in JPA Query
。
@Query("update employee set sal= sal + 2000 where salary < :sal")
void updateEmployeeSalary(@Param("sal") Long sal)
这是我的服务方法
List<Employee> updateSalary(Long sal){
repo.updateEmployeeSalary(sal);
return repo.findAll();
}
您需要@Modifying 注释:
@Modifying
@Query("update employee set sal= sal + 2000 where salary < :sal")
void updateEmployeeSalary(@Param("sal") Long sal)
如果要在Spring Boot JPA
操作中执行Update
和Delete
操作, @Modifying
声明@Modifying
注解
为什么要声明@Modifying
注解 ->增强@Query
注解不仅可以执行 SELECT 查询,还可以执行 INSERT、UPDATE、DELETE 甚至 DDL 查询
如果我们忘记声明@Modifiying
注释怎么@Modifiying
->编译器抛出异常InvalidDataAccessApiUsageException
意味着DML
操作不支持查询。
clearAutomatically=true
定义我们是否应该在执行修改查询后清除底层持久化上下文
修改后的存储库:
@Modifying(clearAutomatically=true)
@query("update Employee set salary = salary + 2000 where salary< :sal")
我认为您需要编写这样的查询
@Modifying
@query("update Employee e set e.salary = e.salary+2000 where e.salary< :sal")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.