Skip to content

JpaRepository#deleteAllByIdInBatch fails when (compound) IdClass keys are used #2414

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
ejl888 opened this issue Jan 21, 2022 · 7 comments
Closed
Labels
type: bug A general bug

Comments

@ejl888
Copy link
Contributor

ejl888 commented Jan 21, 2022

When using an JpaRepository with an compound IdClass the deleteAllByIdInBatch methods fails with an IllegalArgumentException like:

java.lang.IllegalArgumentException: Parameter value element [DeviceGroupDeviceLink.Key(deviceGroupId=8602858a-9ac9-4e70-b381-b2e3163a5b66, deviceId=8cb39b4b-3922-4b65-8150-c5566dc7ffb4)] did not match expected type [java.util.UUID (n/a)],[B@2408dbed,false,2022-01-21 15:08:40.358601)]

Below a test I added to org.springframework.data.jpa.repository.RepositoryWithCompositeKeyTests that fails also (on 2.6.x branch).


@Autowired EntityManager em;

@Test
void shouldSupportDeleteAllByIdInBatchWithIdClass() throws Exception {

    IdClassExampleDepartment dep = new IdClassExampleDepartment();
    dep.setName("TestDepartment");
    dep.setDepartmentId(-1);
    
    IdClassExampleEmployee emp = new IdClassExampleEmployee();
    emp.setDepartment(dep);
    emp = employeeRepositoryWithIdClass.save(emp);
    
    IdClassExampleEmployeePK key = new IdClassExampleEmployeePK(emp.getEmpId(), dep.getDepartmentId());
    assertThat(employeeRepositoryWithIdClass.findById(key)).isNotEmpty();
    
    employeeRepositoryWithIdClass.deleteAllByIdInBatch(Arrays.asList(key));

    em.flush();
    em.clear();
    
    assertThat(employeeRepositoryWithIdClass.findById(key)).isEmpty();
}

Could this be fixed and back ported to the 2.5 branch also? Because we are running Spring Boot 2.5.

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Jan 21, 2022
@ejl888
Copy link
Contributor Author

ejl888 commented Jan 21, 2022

The code in deleteAllInBatch uses a different approach to do the deletion. It creates an OR query. Could this be used as solution for this method also?

As a workaround I use the deleteAllInBatch in combination with getById currently, but I think deleteAllByIdInBatch should handle compound IdClass id's also.

@ejl888 ejl888 changed the title JpaRepository#deleteAllByIdInBatch fails when IdClass keys are used JpaRepository#deleteAllByIdInBatch fails when (compound) IdClass keys are used Jan 21, 2022
@schauder schauder added type: bug A general bug and removed status: waiting-for-triage An issue we've not yet triaged labels Jan 21, 2022
@ejl888
Copy link
Contributor Author

ejl888 commented Jan 21, 2022

Possible quick fix in SimpleJpaRepository

	@Override
	@Transactional
	public void deleteAllByIdInBatch(Iterable<ID> ids) {

		Assert.notNull(ids, "Ids must not be null!");

		if (!ids.iterator().hasNext()) {
			return;
		}

		if (entityInformation.hasCompositeId()) {
			List<T> entities = new ArrayList<>();
                         // XXX Hibernate just creates an empty Entity here when doing the getById. Others might do a real select
                         // that would introduce a big performance penalty.
			ids.forEach(id -> entities.add(getById(id)));
			deleteAllInBatch(entities);
		} else {
			String queryString = String.format(DELETE_ALL_QUERY_BY_ID_STRING, entityInformation.getEntityName(),
					entityInformation.getIdAttribute().getName());

			Query query = em.createQuery(queryString);
			query.setParameter("ids", ids);

			query.executeUpdate();
		}
	}

@ejl888
Copy link
Contributor Author

ejl888 commented Jan 21, 2022

I created a fork with both test and (quick) fix. But do not know how to create a PR/MR (yet).

ejl888 added a commit to ejl888/spring-data-jpa that referenced this issue Jan 21, 2022
Convert ID's to entities and pass them to deleteAllInBatch.

Issue: spring-projects#2414
ejl888 added a commit to ejl888/spring-data-jpa that referenced this issue Jan 21, 2022
Convert ID's to entities and pass them to deleteAllInBatch.

Issue: spring-projects#2414
ejl888 added a commit to ejl888/spring-data-jpa that referenced this issue Jan 25, 2022
Convert ID's to entities and pass them to deleteAllInBatch.

Issue: spring-projects#2414
ejl888 added a commit to ejl888/spring-data-jpa that referenced this issue Jan 25, 2022
Convert ID's to entities and pass them to deleteAllInBatch.

Issue: spring-projects#2414
ejl888 added a commit to ejl888/spring-data-jpa that referenced this issue Jan 25, 2022
Convert ID's to entities and pass them to deleteAllInBatch.

Closes spring-projects#2414
ejl888 added a commit to ejl888/spring-data-jpa that referenced this issue Jan 25, 2022
Convert ID's to entities and pass them to deleteAllInBatch.

Closes spring-projects#2414
ejl888 added a commit to ejl888/spring-data-jpa that referenced this issue Jan 25, 2022
Convert ID's to entities and pass them to deleteAllInBatch.

Closes spring-projects#2414
ejl888 added a commit to ejl888/spring-data-jpa that referenced this issue Jan 25, 2022
Convert ID's to entities and pass them to deleteAllInBatch.

Fixes spring-projects#2414
@ejl888
Copy link
Contributor Author

ejl888 commented Jan 25, 2022

I left the original code intact in case of non compound keys, because the generated in query suits the simple case better.

@ejl888
Copy link
Contributor Author

ejl888 commented Feb 3, 2022

@schauder , I created a PR. Can I ask you to review it or is there another way to ask for review? Or am I just impatient?

schauder added a commit that referenced this issue Feb 8, 2022
Formatting.

See #2414
Original pull request #2419
schauder added a commit that referenced this issue Feb 8, 2022
Formatting.
Fix warnings.

See #2414
Original pull request #2419
schauder added a commit that referenced this issue Feb 8, 2022
Formatting.
Fix warnings.

See #2414
Original pull request #2419
schauder pushed a commit that referenced this issue Feb 8, 2022
Convert ID's to entities and pass them to deleteAllInBatch.

Closes #2414
Original pull request #2419
schauder added a commit that referenced this issue Feb 8, 2022
Formatting.
Fix warnings.

See #2414
Original pull request #2419
@ejl888
Copy link
Contributor Author

ejl888 commented Feb 8, 2022

Thx @schauder

schauder pushed a commit that referenced this issue Feb 8, 2022
Convert ID's to entities and pass them to deleteAllInBatch.

Closes #2414
Original pull request #2419
schauder added a commit that referenced this issue Feb 8, 2022
Formatting.
Fix warnings.

See #2414
Original pull request #2419
schauder pushed a commit that referenced this issue Feb 15, 2022
Convert ID's to entities and pass them to deleteAllInBatch.

Closes #2414
Original pull request #2419
schauder added a commit that referenced this issue Feb 15, 2022
Formatting.
Fix warnings.

See #2414
Original pull request #2419
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: bug A general bug
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants