Matias Paterlini

Helping to make the world more social and connected

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

Comments

funny wallpaper » Hibernate error: “could not execute native bulk manipulation query” said:

Pingback from  funny wallpaper » Hibernate error: “could not execute native bulk manipulation query”

# September 3, 2008 6:09 PM

Gauthier Segay said:

Just as a hint, I would use

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

that should work

# September 4, 2008 3:18 AM

Pascal Forget said:

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.

# September 5, 2008 8:00 AM

Shahriar said:

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

# November 21, 2008 4:50 AM

Diego Arvin said:

Thank you...

# May 27, 2009 10:48 AM

jifei_huang said:

Thanks for your help.

# August 7, 2009 5:22 AM

Pieter said:

Hi Matias,

This is because your SQL statements points to the name of your hibernate bean.  You need to use the name of you database tables.

VotableNode is a hibernate bean name, and not a tablename.

Good luck,

Pieter Pareit

http://www.art-gallery.be

# September 3, 2009 8:30 AM

sriram said:

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

# November 22, 2009 8:40 PM

Esteve said:

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

DELETE FROM table WHERE condition

# November 26, 2009 4:52 AM

Rodolfo Ibarra said:

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

# January 4, 2010 6:56 PM

Ravi said:

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

Good wishes

Ravi

# July 14, 2010 10:08 AM

Alex said:

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

# August 26, 2010 12:03 AM

Math said:

Verify sql execution request in your favorite db client

If success

Just watch InnerException property

# September 24, 2010 8:49 AM

JAVA???????????????» Blog Archive » hibernate3.2?????? said:

Pingback from  JAVA???????????????» Blog Archive » hibernate3.2??????

# October 8, 2010 1:31 PM

ArchMum said:

Hi...

i am using the hibernate query to update the table but getting the error

sql = hibernate query to update fields

we are using rows = em.createNativeQuery(sql).setParameter("investigatorTerm", codingField.getInvestigatorTerm()).setParameter("name", codingField.getName()).executeupdate()

i am getting the following error:

Caused by: javax.persistence.PersistenceException: org.hibernate.exception.SQLGrammarException: could not execute native bulk manipulation query

at org.hibernate.ejb.AbstractEntityManagerImpl.throwPersistenceException(AbstractEntityManagerImpl.java:629)

at org.hibernate.ejb.QueryImpl.executeUpdate(QueryImpl.java:57)

Please help me over this..any suggestions or solution will be of great help to me.

# October 27, 2010 3:20 AM

Bruno Pestalozzi said:

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

Thanks a lot!

# January 12, 2011 4:41 AM

???????????????????????????????????? - Java?????????????????? - [??????:tags] - ????????? | 123Doing said:

Pingback from  ???????????????????????????????????? - Java?????????????????? - [??????:tags] - ????????? | 123Doing

# February 13, 2011 4:49 AM

Danny said:

spot on... Thanks a lot!

# August 9, 2011 3:19 AM

Rashmi said:

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.

# August 17, 2011 12:26 PM

Matt said:

Thnaks, saved me loads of time

# October 14, 2011 10:09 AM

Edgar said:

Great post, it solved my issues :)

# November 30, 2011 5:19 PM

jeroenk-tritac said:

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

# January 2, 2012 6:23 AM

Fred said:

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

# January 13, 2012 10:35 AM

dubi said:

Remove table alias, such as:

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

session.createSQLQuery(del).executeUpdate();

# January 30, 2012 10:09 AM
Leave a Comment

(required) 

(required) 

(optional)

(required)