下面有我的hql:
update User set count = count + ?2 where id = ?1
以下是例外情况:
org.hibernate.hql.internal.ast.QuerySyntaxException: expecting OPEN, found '+' near line 1, column 71 [update com.yitaosoft.edm.common.persist.entity.User set count = count + ? where id = ?]
at org.hibernate.hql.internal.ast.QuerySyntaxException.convert(QuerySyntaxException.java:54)
at org.hibernate.hql.internal.ast.QuerySyntaxException.convert(QuerySyntaxException.java:47)
at org.hibernate.hql.internal.ast.ErrorCounter.throwQueryException(ErrorCounter.java:79)
at org.hibernate.hql.internal.ast.QueryTranslatorImpl.parse(QueryTranslatorImpl.java:278)
at org.hibernate.hql.internal.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:182)
at org.hibernate.hql.internal.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:138)
我想更新用户设置count = count + xx,其中id = xx。但得到了语法错误。为什么?是不是在hql中支持?如何解决这个问题??
答案 0 :(得分:6)
问题是fieldname计数;这是一个保留字,应该引用
Expected OPEN
表示HQL解析器期望count(<expression>)
SQL表达式,而不是count = ...
。
唯一的解决方案是别名User
并强制点缀字段名为:
update User u set u.count = (u.count + ?) where id = ?
答案 1 :(得分:1)
Query query = session.createQuery("update User set count = count + :count" +" where id = :Id");
query.setParameter("Id", id);
query.setParameter("count", count);