Incorrect SQL generated when removing element from a collection when there is a ManyToAny mapping


I've been experiencing an interesting Hibernate issue in my application relating to removing an entity from a collection where there is a ManyToAny mapping from the entity that contains the collection to the interface that defines the members in the collection.

Consider the following mapping from the class "ColorGroup" for a set of "GroupMember"s:

There is also a GroupMembership table that looks like this:

group_id is foreign key'ed to the ColorGroup table and member_id is foreign key'ed to a GroupMember table.

If multiple items (GroupMembers) are added to the "members" set in "ColorGroup", and then one is removed, the following error is encountered:

2012-06-18 16:39:30,685 DEBUG [main] [org.hibernate.SQL] delete from GroupMembership where group_id=? and member_id=?
Hibernate: delete from GroupMembership where group_id=? and member_id=?
2012-06-18 16:39:30,695 WARN [main] [o.h.u.JDBCExceptionReporter] SQL Error: 0, SQLState: S1009
2012-06-18 16:39:30,695 ERROR [main] [o.h.u.JDBCExceptionReporter] Parameter index out of range (3 > number of parameters, which is 2).
2012-06-18 16:39:30,706 ERROR [main] [o.h.e.d.AbstractFlushingEventListener] Could not synchronize database state with session
org.hibernate.exception.GenericJDBCException: could not delete collection rows: [scratch.hibernate.ColorGroup.members#1]
at org.hibernate.exception.SQLStateConverter.handledNonSpecificException( ~[hibernate-core-3.6.0.jar:3.6.0.Final]
at org.hibernate.exception.SQLStateConverter.convert( ~[hibernate-core-3.6.0.jar:3.6.0.Final]
at org.hibernate.exception.JDBCExceptionHelper.convert( ~[hibernate-core-3.6.0.jar:3.6.0.Final]
at org.hibernate.persister.collection.AbstractCollectionPersister.deleteRows( ~[hibernate-core-3.6.0.jar:3.6.0.Final]
at org.hibernate.action.CollectionUpdateAction.execute( ~[hibernate-core-3.6.0.jar:3.6.0.Final]
at org.hibernate.engine.ActionQueue.execute( ~[hibernate-core-3.6.0.jar:3.6.0.Final]
at org.hibernate.engine.ActionQueue.executeActions( ~[hibernate-core-3.6.0.jar:3.6.0.Final]
at org.hibernate.engine.ActionQueue.executeActions( ~[hibernate-core-3.6.0.jar:3.6.0.Final]
at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions( ~[hibernate-core-3.6.0.jar:3.6.0.Final]
at org.hibernate.event.def.DefaultFlushEventListener.onFlush( [hibernate-core-3.6.0.jar:3.6.0.Final]
at org.hibernate.impl.SessionImpl.flush( [hibernate-core-3.6.0.jar:3.6.0.Final]
at org.hibernate.impl.SessionImpl.managedFlush( [hibernate-core-3.6.0.jar:3.6.0.Final]
at org.hibernate.transaction.JDBCTransaction.commit( [hibernate-core-3.6.0.jar:3.6.0.Final]
at scratch.hibernate.MappingTest.testBasicUsage( [bin/:na]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.6.0_17]
at sun.reflect.NativeMethodAccessorImpl.invoke( ~[na:1.6.0_17]
at sun.reflect.DelegatingMethodAccessorImpl.invoke( ~[na:1.6.0_17]
at java.lang.reflect.Method.invoke( ~[na:1.6.0_17]
at junit.framework.TestCase.runTest( [junit-4.6.jar:na]
at junit.framework.TestCase.runBare( [junit-4.6.jar:na]
at junit.framework.TestResult$1.protect( [junit-4.6.jar:na]
at junit.framework.TestResult.runProtected( [junit-4.6.jar:na]
at [junit-4.6.jar:na]
at [junit-4.6.jar:na]
at junit.framework.TestSuite.runTest( [junit-4.6.jar:na]
at [junit-4.6.jar:na]
at [junit-4.6.jar:na]
at [.cp/:na]
at [.cp/:na]
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests( [.cp/:na]
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests( [.cp/:na]
at [.cp/:na]
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main( [.cp/:na]
Caused by: java.sql.SQLException: Parameter index out of range (3 > number of parameters, which is 2).
at com.mysql.jdbc.SQLError.createSQLException( ~[mysql-connector-java-5.1.20.jar:na]
at com.mysql.jdbc.SQLError.createSQLException( ~[mysql-connector-java-5.1.20.jar:na]
at com.mysql.jdbc.SQLError.createSQLException( ~[mysql-connector-java-5.1.20.jar:na]
at com.mysql.jdbc.SQLError.createSQLException( ~[mysql-connector-java-5.1.20.jar:na]
at com.mysql.jdbc.PreparedStatement.checkBounds( ~[mysql-connector-java-5.1.20.jar:na]
at com.mysql.jdbc.PreparedStatement.setInternal( ~[mysql-connector-java-5.1.20.jar:na]
at com.mysql.jdbc.PreparedStatement.setInternal( ~[mysql-connector-java-5.1.20.jar:na]
at com.mysql.jdbc.PreparedStatement.setLong( ~[mysql-connector-java-5.1.20.jar:na]
at org.hibernate.type.descriptor.sql.BigIntTypeDescriptor$1.doBind( ~[hibernate-core-3.6.0.jar:3.6.0.Final]
at org.hibernate.type.descriptor.sql.BasicBinder.bind( ~[hibernate-core-3.6.0.jar:3.6.0.Final]
at org.hibernate.type.AbstractStandardBasicType.nullSafeSet( ~[hibernate-core-3.6.0.jar:3.6.0.Final]
at org.hibernate.type.AbstractStandardBasicType.nullSafeSet( ~[hibernate-core-3.6.0.jar:3.6.0.Final]
at org.hibernate.type.AbstractSingleColumnStandardBasicType.nullSafeSet( ~[hibernate-core-3.6.0.jar:3.6.0.Final]
at org.hibernate.type.AnyType.nullSafeSet( ~[hibernate-core-3.6.0.jar:3.6.0.Final]
at org.hibernate.persister.collection.AbstractCollectionPersister.writeElementToWhere( ~[hibernate-core-3.6.0.jar:3.6.0.Final]
at org.hibernate.persister.collection.AbstractCollectionPersister.deleteRows( ~[hibernate-core-3.6.0.jar:3.6.0.Final]
... 29 common frames omitted

It seems that the correct delete sql is generated: delete from GroupMembership where group_id=? and member_id=?

However, the binding does not appear to be functioning properly because Hibernate is trying to bind a parameter in the third parameter position despite there only being positions for two parameters. Here is an excerpt from a log when more detailed sql logging is enabled:

[org.hibernate.type.descriptor.sql.BasicBinder] binding parameter
[1] as [BIGINT] - 15 [o.h.type.descriptor.sql.BasicBinder] binding
parameter [3] as [BIGINT] - 20 [o.h.util.JDBCExceptionReporter]
Parameter index out of range (3 number of parameters, which is 2).

Tracing through the hibernate code, I noticed in AbstractCollectionPersister.deleteRows() that initially the key ("group_id" in this case) is written to the prepared statement for the deletion in writeKey(). Subsequently, the location of the next parameter to write to the prepared statement is incremented, which is correct. Next, Hibernate goes to write the member_id element to the prepared statement in writeElementToWhere(). Interestingly, the counter is incremented again before the element is written to the prepared statement, thus causing the error.

I've included a zip file that contains a test case (readme, java code, hibernate config) to generate this error.


hibernate-core: 3.6.0
hibernate-infinispan: 3.6.0
hibernate-validator: 4.1.0

MySQL 5.5.11


Brett Meyer
April 7, 2014, 5:48 PM

In an effort to clean up, in bulk, tickets that are most likely out of date, we're transitioning all ORM 3 tickets to an "Awaiting Test Case" state. Please see for more information.

If this is still a legitimate bug in ORM 4, please provide either a test case that reproduces it or enough detail (entities, mappings, snippets, etc.) to show that it still fails on 4. If nothing is received within 3 months or so, we'll be automatically closing them.

Thank you!

Brett Meyer
July 8, 2014, 3:11 PM

Bulk rejecting stale issues. If this is still a legitimate issue on ORM 4, feel free to comment and attach a test case. I'll address responses case-by-case. Thanks!

Heron Berton
July 27, 2020, 8:11 PM

I'm having the same problem (hibernate-core 5.2.7.Final), follows test case from the previous post for validation, was any correction made in future versions?




David Eitel

Fix versions





Suitable for new contributors


Requires Release Note


Pull Request





Affects versions