|
15 | 15 | */
|
16 | 16 | package org.springframework.data.jpa.repository.support;
|
17 | 17 |
|
18 |
| -import static org.springframework.data.jpa.repository.query.QueryUtils.*; |
19 |
| - |
20 |
| -import java.util.ArrayList; |
21 |
| -import java.util.Collection; |
22 |
| -import java.util.Collections; |
23 |
| -import java.util.HashMap; |
24 |
| -import java.util.List; |
25 |
| -import java.util.Map; |
26 |
| -import java.util.Optional; |
27 |
| -import java.util.function.Function; |
28 |
| - |
29 |
| -import javax.persistence.EntityManager; |
30 |
| -import javax.persistence.LockModeType; |
31 |
| -import javax.persistence.NoResultException; |
32 |
| -import javax.persistence.Parameter; |
33 |
| -import javax.persistence.Query; |
34 |
| -import javax.persistence.TypedQuery; |
35 |
| -import javax.persistence.criteria.CriteriaBuilder; |
36 |
| -import javax.persistence.criteria.CriteriaQuery; |
37 |
| -import javax.persistence.criteria.Order; |
38 |
| -import javax.persistence.criteria.ParameterExpression; |
39 |
| -import javax.persistence.criteria.Path; |
40 |
| -import javax.persistence.criteria.Predicate; |
41 |
| -import javax.persistence.criteria.Root; |
42 |
| - |
43 | 18 | import org.springframework.dao.EmptyResultDataAccessException;
|
44 | 19 | import org.springframework.data.domain.Example;
|
45 | 20 | import org.springframework.data.domain.Page;
|
|
62 | 37 | import org.springframework.transaction.annotation.Transactional;
|
63 | 38 | import org.springframework.util.Assert;
|
64 | 39 |
|
| 40 | +import javax.persistence.EntityManager; |
| 41 | +import javax.persistence.LockModeType; |
| 42 | +import javax.persistence.NoResultException; |
| 43 | +import javax.persistence.Parameter; |
| 44 | +import javax.persistence.Query; |
| 45 | +import javax.persistence.TypedQuery; |
| 46 | +import javax.persistence.criteria.CriteriaBuilder; |
| 47 | +import javax.persistence.criteria.CriteriaQuery; |
| 48 | +import javax.persistence.criteria.Order; |
| 49 | +import javax.persistence.criteria.ParameterExpression; |
| 50 | +import javax.persistence.criteria.Path; |
| 51 | +import javax.persistence.criteria.Predicate; |
| 52 | +import javax.persistence.criteria.Root; |
| 53 | +import java.util.ArrayList; |
| 54 | +import java.util.Collection; |
| 55 | +import java.util.Collections; |
| 56 | +import java.util.HashMap; |
| 57 | +import java.util.List; |
| 58 | +import java.util.Map; |
| 59 | +import java.util.Optional; |
| 60 | +import java.util.function.Function; |
| 61 | + |
| 62 | +import static org.springframework.data.jpa.repository.query.QueryUtils.COUNT_QUERY_STRING; |
| 63 | +import static org.springframework.data.jpa.repository.query.QueryUtils.DELETE_ALL_QUERY_BY_ID_STRING; |
| 64 | +import static org.springframework.data.jpa.repository.query.QueryUtils.DELETE_ALL_QUERY_STRING; |
| 65 | +import static org.springframework.data.jpa.repository.query.QueryUtils.applyAndBind; |
| 66 | +import static org.springframework.data.jpa.repository.query.QueryUtils.getQueryString; |
| 67 | +import static org.springframework.data.jpa.repository.query.QueryUtils.toOrders; |
| 68 | + |
65 | 69 | /**
|
66 | 70 | * Default implementation of the {@link org.springframework.data.repository.CrudRepository} interface. This will offer
|
67 | 71 | * you a more sophisticated interface than the plain {@link EntityManager} .
|
@@ -226,13 +230,21 @@ public void deleteAllByIdInBatch(Iterable<ID> ids) {
|
226 | 230 | return;
|
227 | 231 | }
|
228 | 232 |
|
229 |
| - String queryString = String.format(DELETE_ALL_QUERY_BY_ID_STRING, entityInformation.getEntityName(), |
230 |
| - entityInformation.getIdAttribute().getName()); |
| 233 | + if (entityInformation.hasCompositeId()) { |
| 234 | + // XXX Hibernate just creates an empty Entity here when doing the getById. Others might do a real select |
| 235 | + // that would introduce a big performance penalty. See JavaDoc for getById. |
| 236 | + List<T> entities = new ArrayList<>(); |
| 237 | + ids.forEach(id -> entities.add(getById(id))); |
| 238 | + deleteAllInBatch(entities); |
| 239 | + } else { |
| 240 | + String queryString = String.format(DELETE_ALL_QUERY_BY_ID_STRING, entityInformation.getEntityName(), |
| 241 | + entityInformation.getIdAttribute().getName()); |
231 | 242 |
|
232 |
| - Query query = em.createQuery(queryString); |
233 |
| - query.setParameter("ids", ids); |
| 243 | + Query query = em.createQuery(queryString); |
| 244 | + query.setParameter("ids", ids); |
234 | 245 |
|
235 |
| - query.executeUpdate(); |
| 246 | + query.executeUpdate(); |
| 247 | + } |
236 | 248 | }
|
237 | 249 |
|
238 | 250 | /*
|
|
0 commit comments