# Re-using Query instance for multiple executeUpdate calls

**URL:** https://discourse.hibernate.org/t/re-using-query-instance-for-multiple-executeupdate-calls/9880
**Category:** Hibernate ORM
**Created:** [July 3, 2024, 11:02am UTC](https://discourse.hibernate.org/t/re-using-query-instance-for-multiple-executeupdate-calls/9880 "2024-07-03T11:02:55Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![mkomko](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/mkomko/32/2207_2.png) [@mkomko](https://discourse.hibernate.org/u/mkomko)
#### Post date: [July 3, 2024, 11:02am UTC](https://discourse.hibernate.org/t/re-using-query-instance-for-multiple-executeupdate-calls/9880/1 "2024-07-03T11:02:55Z")

</div>

To workaround the limited numbers of SQL parameters that can be passed to databases we are using a set of utility methods to execute a query multiple times with a different set of parameter values. This worked fine in Hibernate 5, but results in an exception using Hibernate 6.

Utility methods:

```java
public static void executeClusteredUpdate(List<?> targets, Query query, String targetsParamName)
{
	int index = 0;
	while (hasMoreElements(targets, index))
	{
		List<?> subTargets = getNextElements(targets, index);
		index += 500;

		query.setParameter(targetsParamName, subTargets).executeUpdate();
	}
}

public static boolean hasMoreElements(Collection<?> objects, int index)
{
	return objects.size() > index;
}

public static <T> List<T> getNextElements(List<T> objects, int index)
{
	return objects.subList(index, Math.min(objects.size(), (index + 500)));
}

```

Entity:

```java
@Entity
@NamedQuery(name = "ExampleEntity.resetNameByIds",
        query= "UPDATE ExampleEntity ee" +
                " SET ee.name = null " +
                "WHERE ee.id IN (:ids)")
public class ExampleEntity {
    @Id
    @GeneratedValue
    private Long id;

    private String name;
}

```

Execution:

```java
List<Long> ids = new ArrayList<>();
for (int i = 0; i < 10000; i++)
{
	ids.add(-1000L);
}

executeClusteredUpdate(ids, entityManager.createNamedQuery("ExampleEntity.resetNameByIds"), "ids");

```

Exception:

```auto
Caused by: java.lang.NullPointerException: Cannot invoke "java.util.List.size()" because "jdbcParamBinds" is null
	at org.hibernate@6.4.4.Final//org.hibernate.query.sqm.internal.SqmUtil.createJdbcParameterBindings(SqmUtil.java:333)
	at org.hibernate@6.4.4.Final//org.hibernate.query.sqm.internal.SimpleUpdateQueryPlan.executeUpdate(SimpleUpdateQueryPlan.java:63)
	at org.hibernate@6.4.4.Final//org.hibernate.query.sqm.internal.QuerySqmImpl.doExecuteUpdate(QuerySqmImpl.java:705)
	at org.hibernate@6.4.4.Final//org.hibernate.query.sqm.internal.QuerySqmImpl.executeUpdate(QuerySqmImpl.java:675)

```

When using the Hibernate Test project, a different exception occurs:

```auto
java.lang.AssertionError
	at org.hibernate.query.sqm.internal.SqmUtil.createJdbcParameterBindings(SqmUtil.java:406)
	at org.hibernate.query.sqm.internal.SimpleUpdateQueryPlan.executeUpdate(SimpleUpdateQueryPlan.java:63)
	at org.hibernate.query.sqm.internal.QuerySqmImpl.doExecuteUpdate(QuerySqmImpl.java:666)
	at org.hibernate.query.sqm.internal.QuerySqmImpl.executeUpdate(QuerySqmImpl.java:639)

```

I’m not able to attach the test project ZIP.

Is this a bug or are we doing something that is not allowed? Do you have a different solution for us? Thank you very much in advance!

---

<div class="post-metadata">

### Author: ![quaff](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/quaff/32/3203_2.png) [@quaff](https://discourse.hibernate.org/u/quaff)
#### Post date: [July 4, 2024, 8:58am UTC](https://discourse.hibernate.org/t/re-using-query-instance-for-multiple-executeupdate-calls/9880/2 "2024-07-04T08:58:19Z")

</div>

It’s fixed by [HHH-18027 Clear expansions NonSelectQueryPlan.executeUpdate call by beikov · Pull Request #8440 · hibernate/hibernate-orm · GitHub](https://github.com/hibernate/hibernate-orm/pull/8440), you should update version to 6.4.9 or 6.5.3.

---

<div class="post-metadata">

### Author: ![mkomko](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/mkomko/32/2207_2.png) [@mkomko](https://discourse.hibernate.org/u/mkomko)
#### Post date: [July 5, 2024, 5:17am UTC](https://discourse.hibernate.org/t/re-using-query-instance-for-multiple-executeupdate-calls/9880/3 "2024-07-05T05:17:07Z")

</div>

Thank you very much for the information! You are indeed correct, although 6.5.3 is not released yet.
