为了账号安全,请及时绑定邮箱和手机立即绑定

我怎样才能修改mysql更新方法?

我怎样才能修改mysql更新方法?

富国沪深 2023-09-27 10:39:36
我想从 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();      }    }  }和例外情况如下:    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
查看完整描述

1 回答

?
MMMHUHU

TA贡献1834条经验 获得超8个赞

您不使用ANDwith SET,而是用逗号分隔它们。正确的语法是

SET item.l1CustomCategoryId = :l1CustomCategoryId, item.l2CustomCategoryId = :l2CustomCategoyId


查看完整回答
反对 回复 2023-09-27
  • 1 回答
  • 0 关注
  • 93 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信