HQL - update query is not woring

3.6k views Asked by At

I have done the Hibernate implementation.Here is a code related to update function.

 Query updateQuery = session.createQuery(" update User set registered = '1' " + " where user_activation_key = '"+userUUID+"'");
 int result = updateQuery.executeUpdate();
  session.getTransaction().commit();

I'm getting this exception

Exception in thread "main" org.hibernate.QueryException: query must begin with SELECT or FROM: update [ update com.shop.domain.User set registered = '1'  where user_activation_key = '04c42f1c-a55d-49cd-8bde-8d340f054d76']
at org.hibernate.QueryException.generateQueryException(QueryException.java:137)
at org.hibernate.QueryException.wrapWithQueryString(QueryException.java:120)
at org.hibernate.hql.internal.classic.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:247)
at org.hibernate.hql.internal.classic.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:209)
at org.hibernate.engine.query.spi.HQLQueryPlan.<init>(HQLQueryPlan.java:126)
at org.hibernate.engine.query.spi.HQLQueryPlan.<init>(HQLQueryPlan.java:88)
at org.hibernate.engine.query.spi.QueryPlanCache.getHQLQueryPlan(QueryPlanCache.java:190)
at org.hibernate.internal.AbstractSessionImpl.getHQLQueryPlan(AbstractSessionImpl.java:301)
at org.hibernate.internal.AbstractSessionImpl.createQuery(AbstractSessionImpl.java:236)
at org.hibernate.internal.SessionImpl.createQuery(SessionImpl.java:1796)
at com.shop.service.ORMService.isUserAvailable(ORMService.java:56)
at com.shop.service.ORMService.activateUserAccount(ORMService.java:39)
at com.shop.web.controller.Test.main(Test.java:10)                    Caused by: org.hibernate.QueryException: query must begin with SELECT or FROM: update
at org.hibernate.hql.internal.classic.ClauseParser.token(ClauseParser.java:104)
at org.hibernate.hql.internal.classic.PreprocessingParser.token(PreprocessingParser.java:131)
at org.hibernate.hql.internal.classic.ParserHelper.parse(ParserHelper.java:61)
at org.hibernate.hql.internal.classic.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:239)
... 10 more

Hibernate.cfg.xml

<hibernate-configuration>
<session-factory>
    <property name="hibernate.bytecode.use_reflection_optimizer">false</property>
    <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
    <property name="hibernate.connection.password">root123</property>
    <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/db</property>
    <property name="hibernate.connection.username">root</property>
    <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>

    <property name="hibernate.show_sql">true</property>

    <property name="hibernate.current_session_context_class">thread</property>
    <property name="hibernate.query.factory_class">org.hibernate.hql.internal.classic.ClassicQueryTranslatorFactory</property>


    <mapping resource="com/shop/domain/User.hbm.xml"></mapping>
</session-factory>

What is this, query must begin with SELECT or FROM: update ? Need some help?

3

There are 3 answers

0
Aniket Kulkarni On BEST ANSWER

In configuration file, change the property hibernate.query.factory_class value
From

org.hibernate.hql.internal.classic.ClassicQueryTranslatorFactory

To

org.hibernate.hql.ast.ASTQueryTranslatorFactory   

See more forum.hibernate.org


Side Note

Use Query.setParameter() method to pass parameter to query

Query updateQuery = session.createQuery("update User set registered = '1' 
                       where user_activation_key = :userUUID");
updateQuery.setParameter("userUUID", userUUID);
0
Bacteria On

Hibernate provides two kinds of HQL parser implementation

  1. org.hibernate.hql.classic.ClassicQueryTranslatorFactory
  2. org.hibernate.hql.ast.ASTQueryTranslatorFactory

and we mentione that in our “hibernate.cfg.xml” configuration file like below

<property name="hibernate.query.factory_class">org.hibernate.hql.classic.ClassicQueryTranslatorFactory</property>      

Or

<property name="hibernate.query.factory_class">org.hibernate.hql.ast.ASTQueryTranslatorFactory</property>

This configuration is optional, if you do not configure the HQL parser implementation explicitly then your default HQL parser implementation will be org.hibernate.hql.ast.ASTQueryTranslatorFactory. To resolve your issue, either remove the entry of hibernate.query.factory_class from your hibernate.cfg.xml file, so that hibernate can use default HQL parser implementation or change it to org.hibernate.hql.ast.ASTQueryTranslatorFactory from org.hibernate.hql.classic.ClassicQueryTranslatorFactory.

1
Mannekenpix On

HQL uses class name instead of table name, and property names instead of column name. I think your issue is related to this.

Do you also import the correct Query?