【问题标题】:HQL - update query is not woringHQL - 更新查询不起作用
【发布时间】:2014-01-30 13:04:41
【问题描述】:

我已经完成了Hibernate的实现。这里是更新功能相关的代码。

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

我遇到了这个异常

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>

这是什么,查询必须以 SELECT 或 FROM: update 开头?需要帮助吗?

【问题讨论】:

标签: java hibernate exception hibernate-batch-updates


【解决方案1】:

在配置文件中,更改属性hibernate.query.factory_class值
来自

org.hibernate.hql.internal.classic.ClassicQueryTranslatorFactory

到

org.hibernate.hql.ast.ASTQueryTranslatorFactory   

查看更多forum.hibernate.org


旁注

使用Query.setParameter()方法传参查询

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

【讨论】:

    【解决方案2】:

    HQL 使用类名代替表名,使用属性名代替列名。我认为您的问题与此有关。

    您是否还导入了正确的查询?

    【讨论】:

    • 是的..我认为这不是问题,因为在另一种方法中 SELECT 查询工作正常。 Query query = session.createQuery(" from User ");. 这很好用
    【解决方案3】:

    Hibernate 提供了两种 HQL 解析器实现

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

    我们在下面的“hibernate.cfg.xml”配置文件中提到了这一点

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

    或者

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

    此配置是可选的,如果您没有明确配置 HQL 解析器实现,那么您的默认 HQL 解析器实现将是 org.hibernate。 hql.ast.ASTQueryTranslatorFactory. 要解决您的问题,请从您的 hibernate.cfg.xml 文件中删除 hibernate.query.factory_class 的条目,以便 hibernate 可以使用默认 HQL 解析器实现或 将其更改为 org.hibernate .hql.ast.ASTQueryTranslatorFactory 来自 org.hibernate.hql.classic.ClassicQueryTranslatorFactory.

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-01-19
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多