Skip to content

Commit b073d00

Browse files
ejl888schauder
authored andcommitted
Support compound IdClass ID's when calling deleteAllByIdInBatch.
Convert ID's to entities and pass them to deleteAllInBatch. Closes #2414 Original pull request #2419
1 parent cbb037e commit b073d00

File tree

2 files changed

+42
-11
lines changed

2 files changed

+42
-11
lines changed

src/main/java/org/springframework/data/jpa/repository/support/SimpleJpaRepository.java

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@
3333
import javax.persistence.TypedQuery;
3434
import javax.persistence.criteria.CriteriaBuilder;
3535
import javax.persistence.criteria.CriteriaQuery;
36-
import javax.persistence.criteria.Order;
3736
import javax.persistence.criteria.ParameterExpression;
3837
import javax.persistence.criteria.Path;
3938
import javax.persistence.criteria.Predicate;
@@ -77,6 +76,7 @@
7776
* @author Jesse Wouters
7877
* @author Greg Turnquist
7978
* @author Yanming Zhou
79+
* @author Ernst-Jan van der Laan
8080
*/
8181
@Repository
8282
@Transactional(readOnly = true)
@@ -222,13 +222,22 @@ public void deleteAllByIdInBatch(Iterable<ID> ids) {
222222
return;
223223
}
224224

225-
String queryString = String.format(DELETE_ALL_QUERY_BY_ID_STRING, entityInformation.getEntityName(),
226-
entityInformation.getIdAttribute().getName());
225+
if (entityInformation.hasCompositeId()) {
226+
// XXX Hibernate just creates an empty Entity when doing the getById.
227+
// Others might do a select right away causing a big performance penalty.
228+
// See JavaDoc for getById.
229+
List<T> entities = new ArrayList<>();
230+
ids.forEach(id -> entities.add(getById(id)));
231+
deleteAllInBatch(entities);
232+
} else {
233+
String queryString = String.format(DELETE_ALL_QUERY_BY_ID_STRING, entityInformation.getEntityName(),
234+
entityInformation.getIdAttribute().getName());
227235

228-
Query query = em.createQuery(queryString);
229-
query.setParameter("ids", ids);
236+
Query query = em.createQuery(queryString);
237+
query.setParameter("ids", ids);
230238

231-
query.executeUpdate();
239+
query.executeUpdate();
240+
}
232241
}
233242

234243
/*
@@ -313,8 +322,6 @@ public Optional<T> findById(ID id) {
313322
/**
314323
* Returns {@link QueryHints} with the query hints based on the current {@link CrudMethodMetadata} and potential
315324
* {@link EntityGraph} information.
316-
*
317-
* @return
318325
*/
319326
protected QueryHints getQueryHints() {
320327
return metadata == null ? NoHints.INSTANCE : DefaultQueryHints.of(entityInformation, metadata);
@@ -552,8 +559,7 @@ public <S extends T> List<S> findAll(Example<S> example) {
552559
*/
553560
@Override
554561
public <S extends T> List<S> findAll(Example<S> example, Sort sort) {
555-
return getQuery(new ExampleSpecification<S>(example, escapeCharacter), example.getProbeType(), sort)
556-
.getResultList();
562+
return getQuery(new ExampleSpecification<S>(example, escapeCharacter), example.getProbeType(), sort).getResultList();
557563
}
558564

559565
/*

src/test/java/org/springframework/data/jpa/repository/RepositoryWithCompositeKeyTests.java

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,10 @@
2020
import java.util.Arrays;
2121
import java.util.List;
2222

23+
import javax.persistence.EntityManager;
24+
2325
import org.junit.jupiter.api.Test;
2426
import org.junit.jupiter.api.extension.ExtendWith;
25-
2627
import org.springframework.beans.factory.annotation.Autowired;
2728
import org.springframework.data.domain.Page;
2829
import org.springframework.data.domain.PageRequest;
@@ -47,6 +48,7 @@
4748
* @author Thomas Darimont
4849
* @author Mark Paluch
4950
* @author Jens Schauder
51+
* @author Ernst-Jan van der Laan
5052
*/
5153
@ExtendWith(SpringExtension.class)
5254
@ContextConfiguration(classes = SampleConfig.class)
@@ -55,6 +57,7 @@ public class RepositoryWithCompositeKeyTests {
5557

5658
@Autowired EmployeeRepositoryWithIdClass employeeRepositoryWithIdClass;
5759
@Autowired EmployeeRepositoryWithEmbeddedId employeeRepositoryWithEmbeddedId;
60+
@Autowired EntityManager em;
5861

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

132+
@Test // DATAJPA-2414
133+
void shouldSupportDeleteAllByIdInBatchWithIdClass() throws Exception {
134+
135+
IdClassExampleDepartment dep = new IdClassExampleDepartment();
136+
dep.setName("TestDepartment");
137+
dep.setDepartmentId(-1);
138+
139+
IdClassExampleEmployee emp = new IdClassExampleEmployee();
140+
emp.setDepartment(dep);
141+
emp = employeeRepositoryWithIdClass.save(emp);
142+
143+
IdClassExampleEmployeePK key = new IdClassExampleEmployeePK(emp.getEmpId(), dep.getDepartmentId());
144+
assertThat(employeeRepositoryWithIdClass.findById(key)).isNotEmpty();
145+
146+
employeeRepositoryWithIdClass.deleteAllByIdInBatch(Arrays.asList(key));
147+
148+
em.flush();
149+
em.clear();
150+
151+
assertThat(employeeRepositoryWithIdClass.findById(key)).isEmpty();
152+
}
153+
129154
@Test // DATAJPA-497
130155
void sortByEmbeddedPkFieldInCompositePkWithEmbeddedIdInQueryDsl() {
131156

0 commit comments

Comments
 (0)