Java Hibernate org.hibernate.exception.SQLGrammarException:无法在createSQLQuery

时间:2017-06-20 00:23:44

标签: java mysql sql hibernate select

我有这种方法。

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

4 个答案:

答案 0 :(得分:3)

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();

答案 1 :(得分:2)

没有"受影响的身份"在UPDATE声明中。

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

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

PRIMARY KEY的{​​{1}}是什么?我们说它是student。要检索studentId值的所有(如果有的话),你需要将Hibernate等效于这个伪代码:

studentId

更多

该代码可以捆绑在一个存储过程中,从而允许它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; 就好像一个语句一样。 (我不知道如何使用Hibernate。)

答案 2 :(得分:1)

我想,你无法以Hibernate的方式制作它。

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

答案 3 :(得分:1)

我会提取要作为实体列表更新的提取记录,然后你可以迭代设置值,持久化并仍然在方法结束时返回受感染的ID