Hibernate error: "could not execute native bulk manipulation query"

I was getting this error:

exception: org.hibernate.exception.SQLGrammarException: could not execute native bulk manipulation query

After looking in more than 25 google results I couldn't find any clue about what could be happening. But somehow I got illuminated and found what was happening. That's why I'm writing this post.

This was my code before I saw the problem:

  1. String del = "DELETE VotableNode vn WHERE vn.votingSetBucketId = " + Integer.toString(bucketId);
  2. session.createSQLQuery(del).executeUpdate();

Let Me tell you something about this error. I'm not a hibernate crack, but as you can see it's saying that you can't create a bulk delete on a sql query by using hibernate.

The error in this code is that I was executing a HQL script using createSQLQuery method which is completely wrong, I should use createQuery.

  1. String del = "DELETE VotableNode vn WHERE vn.votingSetBucketId = " + Integer.toString(bucketId);
  2. session.createQuery(del).executeUpdate();

So there's nothing much to say, If you want to make a bulk task like an update of multiple records or a deletion of multiple rows using a simple query, you MUST use a HQL query.

Hope that helps a little!

Matias Paterlini

25 Comments

  • Just as a hint, I would use

    session.CreateSqlQuery("DELETE VotableNode vn WHERE vn.votingSetBucketId = :bucketId").SetParameter("bucketId", buckedId).ExecuteUpdate();

    that should work

  • Actually, I am able to do a bulk delete using Hibernate on a PostgreSQL 8.3, but it doesn't work on PostgreSQL 8.0.

  • Thanks man you saved me from alot of trouble on searching the bloody exception error.

  • Thanks for your help.

  • Thanks a lot for your help. I found your page as the first result from google and saved me tones of time.

  • Maybe this is only a syntax error, your SQL code is missing a FROM:

    DELETE FROM table WHERE condition

  • Thanks a lot, i found this very useful because the help provided by hibernate is not very complete

  • Thanks Matias, It solved my problem and saved lot of my time.

    Good wishes
    Ravi

  • This is a sql syntax error.
    For SQL Server, it is not allowed to use alias for the effected table in delete or update statement.
    For DB2, it is allowed.

    DELETE FROM table WHERE condition -> ok
    DELETE table WHERE condition -> ok
    DELETE table as t WHERE condition -> not ok for SQL Server

  • Verify sql execution request in your favorite db client
    If success
    Just watch InnerException property

  • I was confronted with the same error and could eliminate it without a hastle due to your hint.
    Thanks a lot!

  • spot on... Thanks a lot!

  • This error also occurs if input parameters are of different data type. May be a typo. for instance, if you input data of type String instead of int.

  • Thnaks, saved me loads of time

  • Great post, it solved my issues :)

  • You can use CreateSQLQuery for MS Sql Server is you add "MultipleActiveResultSets=True" to you connection string

  • I had the same problem but on a postgres bulk delete sql statement across multiple tables with a parameter passed in for the id. Turns out it was not that it cant be done with hibernate SQLQuery, but that the add parameter to the query was making the sql use double quotes. Postgres native sql requires single quotes for strings and in my case the id is a string.

    Changing my code to do this worked:

    String sql = "blah blah :param; blah blah :param";
    sql = sql.replaceAll(":param", "'" + id + "'");

    session.createSQLQuery(sql).executeUpdate();

    -Fred

  • Remove table alias, such as:

    String del = "DELETE VotableNode WHERE votingSetBucketId = " + Integer.toString(bucketId);"
    session.createSQLQuery(del).executeUpdate();

  • General Error:javax.ejb.EJBTranactionRolledbackException:org.hibernate.SQLGrammarException:could not execute native bulk

  • your says only delete,this problem is insertion also please read once hibernate

  • Very useful... i solved my problem through this post.

    Thanks

  • wqrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr

  • Only for Oracle Database

    Those who get the "javax.persistence.PersistenceException: org.hibernate.exception.SQLGrammarException: could not execute native bulk manipulation query" message, may use this solution to solve problem:

    em.getTransaction().begin();
    em.createNativeQuery("BEGIN update tblTEST set fVal = '123' WHERE fYear = '2012'; END;").executeUpdate();
    em.getTransaction().commit();

  • Im a newbie in java/hibernate/mysql. I got the error while calling the following stored procedure. The SP works fine alone so i guess im making call in a wrong way.

    String opt="REFUND";
    Query query = this.getSession().createSQLQuery("CALL insertComm (:remitNo, :opt)")
    .setParameter("remitNo", remitNo)
    .setParameter("opt", opt);
    query.executeUpdate();


    I am seeing same kind of error. Something about executing in bulk.

  • Great article post.Really looking forward to read more. Cool.

Comments have been disabled for this content.