【问题标题】:Java Hibernate org.hibernate.exception.SQLGrammarException: could not extract ResultSet on createSQLQueryJava Hibernate org.hibernate.exception.SQLGrammarException:无法在 createSQLQuery 上提取 ResultSet
【发布时间】:2017-11-22 08:04:08
【问题描述】:

我有这个方法。

private final void updateAllTableFields(final Class clazz){
    final String tableName = ((Table)clazz.getAnnotation(Table.class)).name();
    final String sqlQuery = new StringBuilder("SET @ids = NULL; ")
            .append("UPDATE ")
            .append(tableName)
            .append(' ')
            .append("set activeRecord=:activeRecord ")
            .append("where activeRecord=true and updateable=true ")
            .append("and (SELECT @ids \\:= CONCAT_WS(',', id, @ids)); ")
            .append("select @ids;")
            .toString();
    final Query query = session.createSQLQuery(sqlQuery)
            .setParameter("activeRecord",Boolean.FALSE);
    final Object idsList=query.uniqueResult();
    System.out.println("idsList = " + idsList);
}        

我想进行更新并返回受影响的 Id 这工作完美使用 rawSQL 以字符串方式返回 id 但我无法使用 Hibernate 任何提示使其工作!!!

在此先致谢并致以最诚挚的问候。

更新

我需要进行更新并返回受影响的 ID!我不想做一个简单的更新。

你可以check it out the original question here pal: https://stackoverflow.com/questions/44604763/java-hibernate-tips-about-update-all-table-fields-performance

更新 错误是

at org.hibernate.exception.internal.SQLExceptionTypeDelegate.convert(SQLExceptionTypeDelegate.java:80)
at org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:49)
at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:126)
at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:112)
at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.extract(ResultSetReturnImpl.java:89)
at org.hibernate.loader.Loader.getResultSet(Loader.java:2065)
at org.hibernate.loader.Loader.executeQueryStatement(Loader.java:1862)
at org.hibernate.loader.Loader.executeQueryStatement(Loader.java:1838)
at org.hibernate.loader.Loader.doQuery(Loader.java:909)
at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:354)
at org.hibernate.loader.Loader.doList(Loader.java:2553)
at org.hibernate.loader.Loader.doList(Loader.java:2539)
at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2369)
at org.hibernate.loader.Loader.list(Loader.java:2364)
at org.hibernate.loader.custom.CustomLoader.list(CustomLoader.java:353)
at org.hibernate.internal.SessionImpl.listCustomQuery(SessionImpl.java:1873)
at org.hibernate.internal.AbstractSessionImpl.list(AbstractSessionImpl.java:311)
at org.hibernate.internal.SQLQueryImpl.list(SQLQueryImpl.java:141)
at org.hibernate.internal.AbstractQueryImpl.uniqueResult(AbstractQueryImpl.java:966)
at company.nuevemil.code.finalizarEntornoDePrueba(Test.java:56)
at company.nuevemil.code.main(Test.java:27)


Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'UPDATE student set activeRecord=false,uid=1 where activeRecord=true at line 1

【问题讨论】:

  • 介意发布您在尝试时遇到的错误吗?
  • 已经完成了,非常感谢
  • 请在您提供的代码中提供变量activeRecord 的值。如果只是true,那么您提供的代码确实没有生成您提供的错误消息。

标签: java mysql sql hibernate select


【解决方案1】:

我建议提取要更新为实体列表的记录,然后您可以迭代设置值、持久化并在方法结束时仍然返回受影响的 id

【讨论】:

    【解决方案2】:

    UPDATE 语句中没有“受影响的 id”。

    UPDATE student
        set activeRecord=false,uid=1
        where activeRecord=true
    

    可以修改 0 行、1 行或多行。

    studentPRIMARY KEY 是什么?假设它是studentId。要检索所有(如果有)studentId 值,您需要此伪代码的 Hibernate 等效项:

    START TRANSACTION;
    @ids = SELECT studentId
               FROM student
               WHERE activeRecord=true  -- and updateable=true ??
               FOR UPDATE;
    UPDATE student
        SET activeRecord=false,
            uid=1
        WHERE activeRecord=true  -- and updateable=true ??
        ;
    COMMIT;
    

    更多

    该代码可以捆绑在一个存储过程中,从而使其成为CALLed,就像一条语句一样。 (我不知道如何使它与 Hibernate 一起工作。)

    【讨论】:

    • 我的 SQL 工作我想用 hibernate 来实现它,但恐怕是不可能的......
    • 您的伪代码可以工作,但现在我想在单个查询中执行的 2 个查询中的实现方式正是如此。
    • @chiperortiz - 我添加了一条线索。
    【解决方案3】:

    我想,你将无法以 Hibernate 方式实现它。

    Hibernate 独立于数据库。但是初始化变量的查询部分(我的意思是set @ids = null;)不能在所有关系数据库中移植,所以我不希望它在某个地方出现在 Hibernate API 中。

    【讨论】:

    • @chiperortiz 是的,但是如何为特定数据库提供 Hibernate API?在我看来这是不可能的
    【解决方案4】:
    you have to use HQL Query for bulk update. you are going write way only thing is that, you have to create HQL query for example
    
    
        Your Query Might be like this:-
        final String tableName = ((Table)clazz.getAnnotation(Table.class)).name();
            final String sqlQuery = new StringBuilder("SET @ids = NULL; ")
                    .append("UPDATE ")
                    .append(tableName)
                    .append(' ')
                    .append("set activeRecord=:activeRecord ")
                    .append("where activeRecord=true and updateable=true ")
                    .append("and (SELECT @ids \\:= CONCAT_WS(',', id, @ids)); ")
                    .append("select @ids;")
                    .toString();
            final Query query = session.createQuery(sqlQuery)
                    .setParameter("activeRecord",Boolean.FALSE);
            final Object idsList=query.executeUpdate();
    
        Example Query:
        final String tableName = ((Table)clazz.getAnnotation(Table.class)).name();
           Query qry = session.createQuery("update "+tableName+" p set p.proName=?
        where p.productId=111");
                    qry.setParameter(0,"updated..");
                    int res = qry.executeUpdate();
    

    【讨论】:

    猜你喜欢
    • 2015-08-03
    • 2018-10-04
    • 1970-01-01
    • 2016-02-17
    • 2015-05-27
    • 2016-05-06
    • 2015-07-22
    • 2014-07-20
    相关资源
    最近更新 更多