Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hibernate throws a expecting OPEN, found '+'

Tags:

java

hibernate

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 ??

like image 798
Mr rain Avatar asked Mar 03 '14 08:03

Mr rain


2 Answers

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 = ?

like image 100
Luca Basso Ricci Avatar answered Nov 08 '22 02:11

Luca Basso Ricci


Query query = session.createQuery("update User set count = count + :count" +" where id = :Id");
query.setParameter("Id", id);
query.setParameter("count", count);
like image 45
Srinivas Avatar answered Nov 08 '22 02:11

Srinivas