Support compound IdClass ID's when calling deleteAllByIdInBatch.
Convert ID's to entities and pass them to deleteAllInBatch. Closes #2414 Original pull request #2419
This commit is contained in:
committed by
Jens Schauder
parent
cbb037e026
commit
b073d00739
@@ -33,7 +33,6 @@ import javax.persistence.Query;
|
||||
import javax.persistence.TypedQuery;
|
||||
import javax.persistence.criteria.CriteriaBuilder;
|
||||
import javax.persistence.criteria.CriteriaQuery;
|
||||
import javax.persistence.criteria.Order;
|
||||
import javax.persistence.criteria.ParameterExpression;
|
||||
import javax.persistence.criteria.Path;
|
||||
import javax.persistence.criteria.Predicate;
|
||||
@@ -77,6 +76,7 @@ import org.springframework.util.Assert;
|
||||
* @author Jesse Wouters
|
||||
* @author Greg Turnquist
|
||||
* @author Yanming Zhou
|
||||
* @author Ernst-Jan van der Laan
|
||||
*/
|
||||
@Repository
|
||||
@Transactional(readOnly = true)
|
||||
@@ -222,13 +222,22 @@ public class SimpleJpaRepository<T, ID> implements JpaRepositoryImplementation<T
|
||||
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();
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -313,8 +322,6 @@ public class SimpleJpaRepository<T, ID> implements JpaRepositoryImplementation<T
|
||||
/**
|
||||
* Returns {@link QueryHints} with the query hints based on the current {@link CrudMethodMetadata} and potential
|
||||
* {@link EntityGraph} information.
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
protected QueryHints getQueryHints() {
|
||||
return metadata == null ? NoHints.INSTANCE : DefaultQueryHints.of(entityInformation, metadata);
|
||||
@@ -552,8 +559,7 @@ public class SimpleJpaRepository<T, ID> implements JpaRepositoryImplementation<T
|
||||
*/
|
||||
@Override
|
||||
public <S extends T> List<S> findAll(Example<S> example, Sort sort) {
|
||||
return getQuery(new ExampleSpecification<S>(example, escapeCharacter), example.getProbeType(), sort)
|
||||
.getResultList();
|
||||
return getQuery(new ExampleSpecification<S>(example, escapeCharacter), example.getProbeType(), sort).getResultList();
|
||||
}
|
||||
|
||||
/*
|
||||
|
||||
@@ -20,9 +20,10 @@ import static org.assertj.core.api.Assertions.*;
|
||||
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;
|
||||
@@ -47,6 +48,7 @@ import org.springframework.transaction.annotation.Transactional;
|
||||
* @author Thomas Darimont
|
||||
* @author Mark Paluch
|
||||
* @author Jens Schauder
|
||||
* @author Ernst-Jan van der Laan
|
||||
*/
|
||||
@ExtendWith(SpringExtension.class)
|
||||
@ContextConfiguration(classes = SampleConfig.class)
|
||||
@@ -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
|
||||
@@ -126,6 +129,28 @@ public class RepositoryWithCompositeKeyTests {
|
||||
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() {
|
||||
|
||||
|
||||
Reference in New Issue
Block a user