首页 > 解决方案 > 用二元运算符 mysql 查询更新列的 JPQL jpa (@Query) 是什么?

问题描述

SQL查询:

update de_meta set service_lines = service_lines | 5 where de_id = 20;

这在 SQL 控制台上运行良好。

JPA 查询:

@Modifying
@Query("update DeMetaEntity set serviceLines = serviceLines | ?2 where deId = ?1")
void addDeServiceLineMap(Integer deId, int serviceLine);

此 JPA 查询引发错误,因为 | (按位或)在 JPQL 中无效。有没有办法为给定的 SQL 查询编写等效的 JPQL?

我不想使用标准查询。当我在 JAVA INTERFACE 使用它时。

标签: mysqlspring-bootjpajpql

解决方案


按照 Alan 的建议创建本机查询:

@Modifying
@Query("update de_meta set service_lines = service_lines | ?2 where de_id = ?1", nativeQuery=true)
void addDeServiceLineMap(Integer deId, int serviceLine);

开关 nativeQuery=true 将按原样执行 SQL 查询。


推荐阅读