Java Hibernate 抛出一个预期的 OPEN,发现“+”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22142224/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Hibernate throws a expecting OPEN, found '+'
提问by Mr rain
there is my hql below:
下面是我的 hql:
update User set count = count + ?2 where id = ?1
and there is exception details below:
以下是例外详情:
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)
I want to update user set count = count + xx where id = xx. but got a syntax error. why ? is it not support in hql ?? how to solve this issue ??
我想更新用户集 count = count + xx 其中 id = xx。但出现语法错误。为什么 ?hql 不支持吗??这个问题怎么解决??
回答by Srinivas
Query query = session.createQuery("update User set count = count + :count" +" where id = :Id");
query.setParameter("Id", id);
query.setParameter("count", count);
回答by Luca Basso Ricci
the problem is the fieldname count; it is a reserved word and should be quoted.Expected OPEN
means HQL parser is expecting count(<expression>)
SQL expression and not count = ...
.
The only solution is to alias User
and force dotted fieldname as:
问题是字段名计数;它是一个保留字,应该被引用。Expected OPEN
意味着 HQL 解析器需要count(<expression>)
SQL 表达式而不是count = ...
. 唯一的解决方案是别名User
和强制虚线字段名称为:
update User u set u.count = (u.count + ?) where id = ?
update User u set u.count = (u.count + ?) where id = ?