Skip to content

Support compound IdClass ID's when calling deleteAllByIdInBatch. #2419

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
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@
* @author Jesse Wouters
* @author Greg Turnquist
* @author Yanming Zhou
* @author Ernst-Jan van der Laan
*/
@Repository
@Transactional(readOnly = true)
Expand Down Expand Up @@ -226,13 +227,22 @@ public void deleteAllByIdInBatch(Iterable<ID> ids) {
return;
}

String queryString = String.format(DELETE_ALL_QUERY_BY_ID_STRING, entityInformation.getEntityName(),
entityInformation.getIdAttribute().getName());
if (entityInformation.hasCompositeId()) {
// XXX Hibernate just creates an empty Entity when doing the getById.
// Others might do a select right away causing a big performance penalty.
// See JavaDoc for getById.
List<T> entities = new ArrayList<>();
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 query = em.createQuery(queryString);
query.setParameter("ids", ids);

query.executeUpdate();
query.executeUpdate();
}
}

/*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,10 @@
import java.util.Arrays;
import java.util.List;

import javax.persistence.EntityManager;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
Expand All @@ -47,6 +48,7 @@
* @author Thomas Darimont
* @author Mark Paluch
* @author Jens Schauder
* @author Ernst-Jan van der Laan
*/
@ExtendWith(SpringExtension.class)
@ContextConfiguration(classes = SampleConfig.class)
Expand All @@ -55,6 +57,7 @@ public class RepositoryWithCompositeKeyTests {

@Autowired EmployeeRepositoryWithIdClass employeeRepositoryWithIdClass;
@Autowired EmployeeRepositoryWithEmbeddedId employeeRepositoryWithEmbeddedId;
@Autowired EntityManager em;

/**
* @see <a href="download.oracle.com/otn-pub/jcp/persistence-2_1-fr-eval-spec/JavaPersistence.pdf">Final JPA 2.0
Expand Down Expand Up @@ -126,6 +129,28 @@ void shouldSupportFindAllWithPageableAndEntityWithIdClass() throws Exception {
assertThat(page.getTotalElements()).isEqualTo(1L);
}

@Test // DATAJPA-2414
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();
}

@Test // DATAJPA-497
void sortByEmbeddedPkFieldInCompositePkWithEmbeddedIdInQueryDsl() {

Expand Down