there is my hql below:
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 ??
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:
update User u set u.count = (u.count + ?) where id = ?
Query query = session.createQuery("update User set count = count + :count" +" where id = :Id");
query.setParameter("Id", id);
query.setParameter("count", count);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With