首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何修改mysql更新方法?

如何修改mysql更新方法?
EN

Stack Overflow用户
提问于 2019-10-04 15:06:45
回答 1查看 36关注 0票数 0

我想从dto列表更新mysql表,但有一些例外,比如语法问题,下面是我的代码:

代码语言:javascript
运行
复制
@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();
      }
    }
  }

和exception如下:

代码语言:javascript
运行
复制
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
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-10-04 15:09:05

您不能在SET中使用AND,您可以用逗号将它们分开。正确的语法是

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

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/58231479

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档