java - 如何修改mysql更新方法?
问题描述
我想从 dto 列表中更新 mysql 表,但是有一些例外,比如语法问题,这是我的代码:
@Override
@Transactional
public void updateCustomCategory(List<ItemDto> itemDtoList) {
if (CollectionUtils.isNotEmpty(itemDtoList)) {
for (ItemDto itemDto : itemDtoList) {
Long l1CustomCategoryId = itemDto.getL1CustomCatId();
Long l2CustomCategoyId = itemDto.getL2CustomCatId();
StringBuilder query =
new StringBuilder(
"update ItemDto item set item.l1CustomCategoryId = :l1CustomCategoryId and item.l2CustomCategoryId = :l2CustomCategoyId where itemId = :itemId");
Query q = this.em.createQuery(query.toString());
q.setParameter("l1CustomCategoryId", l1CustomCategoryId);
q.setParameter("l2CustomCategoyId", l2CustomCategoyId);
q.setParameter("itemId", itemDto.getItemId());
q.executeUpdate();
}
}
}
例外如下:
2019-10-04 14:49:00.227 ERROR 11909 --- [nio-9092-exec-5] o.a.c.c.C.[.[.[/].[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.dao.InvalidDataAccessApiUsageException: org.hibernate.hql.internal.ast.QuerySyntaxException: unexpected token: and near line 1, column 108 [update com.shopee.data.dto.brandseller.item.ItemDto item set item.l1CustomCategoryId = :l1CustomCategoryId and item.l2CustomCategoryId = :l2CustomCategoyId where itemId = :itemId]; nested exception is java.lang.IllegalArgumentException: org.hibernate.hql.internal.ast.QuerySyntaxException: unexpected token: and near line 1, column 108 [update com.shopee.data.dto.brandseller.item.ItemDto item set item.l1CustomCategoryId = :l1CustomCategoryId and item.l2CustomCategoryId = :l2CustomCategoyId where itemId = :itemId]] with root cause
org.hibernate.hql.internal.ast.QuerySyntaxException: unexpected token: and near line 1, column 108 [update com.shopee.data.dto.brandseller.item.ItemDto item set item.l1CustomCategoryId = :l1CustomCategoryId and item.l2CustomCategoryId = :l2CustomCategoyId where itemId = :itemId]
at org.hibernate.hql.internal.ast.QuerySyntaxException.convert(QuerySyntaxException.java:74) ~[hibernate-core-5.3.7.Final.jar!/:5.3.7.Final]
at org.hibernate.hql.internal.ast.ErrorTracker.throwQueryException(ErrorTracker.java:93) ~[hibernate-core-5.3.7.Final.jar!/:5.3.7.Final]
at org.hibernate.hql.internal.ast.QueryTranslatorImpl.parse(QueryTranslatorImpl.java:296) ~[hibernate-core-5.3.7.Final.jar!/:5.3.7.Final]
at org.hibernate.hql.internal.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:188) ~[hibernate-core-5.3.7.Final.jar!/:5.3.7.Final]
at org.hibernate.hql.internal.ast.QueryTranslatorImpl.compile
解决方案
你不使用AND
with SET
,你用逗号分隔它们。正确的语法是
SET item.l1CustomCategoryId = :l1CustomCategoryId, item.l2CustomCategoryId = :l2CustomCategoyId
推荐阅读
- javascript - IE 11 中的 React+Material-UI 文本字段 defaultValue 问题
- c - 为什么在达到“A Spade”之前抽牌会输出不同的结果
- python - 如何更改 Pandas 系列的索引值
- php - 如何在php中将来自不同用户的相同文档上传到服务器?
- python - pabot:: 是否可以发送 --variablefile <
>而不是--argumentfile选项中的--variable key:value? - arduino - 无传感器 BLDC 电机控制 BEMF 部分
- xaml - 如何将 TextChanged 事件添加到 xamarin.forms 中的标签?
- caching - 在具有 64 字节高速缓存行的 32kB(32,768 字节)直接映射回写高速缓存中,用于数据的位的百分比是多少?
- c++11 - 如何删除以下代码中的分段错误?
- image - 我有一张 600x1000 的图片,我想使用 pygame 将图片的底部部分粘贴到我的 600x600 窗口上