Support derived delete.
Closes #771 See #230 Original pull request #1486
This commit is contained in:
committed by
Jens Schauder
parent
721a7ad458
commit
1515a3af42
@@ -20,6 +20,7 @@ import static org.assertj.core.api.Assertions.*;
|
||||
|
||||
import java.sql.SQLException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
@@ -216,28 +217,52 @@ public class JdbcRepositoryEmbeddedWithCollectionIntegrationTests {
|
||||
assertThat(repository.findAll()).isEmpty();
|
||||
}
|
||||
|
||||
private static DummyEntity createDummyEntity() {
|
||||
DummyEntity entity = new DummyEntity();
|
||||
entity.setTest("root");
|
||||
@Test // DATAJDBC-551
|
||||
public void deleteByTest() {
|
||||
|
||||
final Embeddable embeddable = new Embeddable();
|
||||
embeddable.setTest("embedded");
|
||||
DummyEntity one = repository.save(createDummyEntity("root1"));
|
||||
DummyEntity two = repository.save(createDummyEntity("root2"));
|
||||
DummyEntity three = repository.save(createDummyEntity("root3"));
|
||||
|
||||
final DummyEntity2 dummyEntity21 = new DummyEntity2();
|
||||
dummyEntity21.setTest("entity1");
|
||||
assertThat(repository.deleteByTest(two.getTest())).isEqualTo(1);
|
||||
|
||||
final DummyEntity2 dummyEntity22 = new DummyEntity2();
|
||||
dummyEntity22.setTest("entity2");
|
||||
assertThat(repository.findAll()) //
|
||||
.extracting(DummyEntity::getId) //
|
||||
.containsExactlyInAnyOrder(one.getId(), three.getId());
|
||||
|
||||
embeddable.getList().add(dummyEntity21);
|
||||
embeddable.getList().add(dummyEntity22);
|
||||
Long count = template.queryForObject("select count(1) from dummy_entity2", Collections.emptyMap(), Long.class);
|
||||
assertThat(count).isEqualTo(4);
|
||||
|
||||
entity.setEmbeddable(embeddable);
|
||||
}
|
||||
|
||||
return entity;
|
||||
}
|
||||
private static DummyEntity createDummyEntity() {
|
||||
return createDummyEntity("root");
|
||||
}
|
||||
|
||||
interface DummyEntityRepository extends CrudRepository<DummyEntity, Long> {}
|
||||
private static DummyEntity createDummyEntity(String test) {
|
||||
DummyEntity entity = new DummyEntity();
|
||||
entity.setTest(test);
|
||||
|
||||
final Embeddable embeddable = new Embeddable();
|
||||
embeddable.setTest("embedded");
|
||||
|
||||
final DummyEntity2 dummyEntity21 = new DummyEntity2();
|
||||
dummyEntity21.setTest("entity1");
|
||||
|
||||
final DummyEntity2 dummyEntity22 = new DummyEntity2();
|
||||
dummyEntity22.setTest("entity2");
|
||||
|
||||
embeddable.getList().add(dummyEntity21);
|
||||
embeddable.getList().add(dummyEntity22);
|
||||
|
||||
entity.setEmbeddable(embeddable);
|
||||
|
||||
return entity;
|
||||
}
|
||||
|
||||
interface DummyEntityRepository extends CrudRepository<DummyEntity, Long> {
|
||||
int deleteByTest(String test);
|
||||
}
|
||||
|
||||
private static class DummyEntity {
|
||||
@Column("ID")
|
||||
|
||||
@@ -0,0 +1,132 @@
|
||||
package org.springframework.data.jdbc.repository;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.springframework.test.context.TestExecutionListeners.MergeMode.MERGE_WITH_DEFAULTS;
|
||||
|
||||
import lombok.Data;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.annotation.Import;
|
||||
import org.springframework.data.annotation.Id;
|
||||
import org.springframework.data.jdbc.repository.support.JdbcRepositoryFactory;
|
||||
import org.springframework.data.jdbc.testing.AssumeFeatureTestExecutionListener;
|
||||
import org.springframework.data.jdbc.testing.TestConfiguration;
|
||||
import org.springframework.data.repository.CrudRepository;
|
||||
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.TestExecutionListeners;
|
||||
import org.springframework.test.context.junit.jupiter.SpringExtension;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
/**
|
||||
* Integration tests with collections chain.
|
||||
*
|
||||
* @author Yunyoung LEE
|
||||
*/
|
||||
@ContextConfiguration
|
||||
@Transactional
|
||||
@TestExecutionListeners(value = AssumeFeatureTestExecutionListener.class, mergeMode = MERGE_WITH_DEFAULTS)
|
||||
@ExtendWith(SpringExtension.class)
|
||||
public class JdbcRepositoryWithCollectionsChainIntegrationTests {
|
||||
|
||||
@Autowired NamedParameterJdbcTemplate template;
|
||||
@Autowired DummyEntityRepository repository;
|
||||
|
||||
private static DummyEntity createDummyEntity() {
|
||||
|
||||
DummyEntity entity = new DummyEntity();
|
||||
entity.setName("Entity Name");
|
||||
return entity;
|
||||
}
|
||||
|
||||
@Test // DATAJDBC-551
|
||||
public void deleteByName() {
|
||||
|
||||
ChildElement element1 = createChildElement("one");
|
||||
ChildElement element2 = createChildElement("two");
|
||||
|
||||
DummyEntity entity = createDummyEntity();
|
||||
entity.content.add(element1);
|
||||
entity.content.add(element2);
|
||||
|
||||
entity = repository.save(entity);
|
||||
|
||||
assertThat(repository.deleteByName("Entity Name")).isEqualTo(1);
|
||||
|
||||
assertThat(repository.findById(entity.id)).isEmpty();
|
||||
|
||||
Long count = template.queryForObject("select count(1) from grand_child_element", new HashMap<>(), Long.class);
|
||||
assertThat(count).isEqualTo(0);
|
||||
}
|
||||
|
||||
private ChildElement createChildElement(String name) {
|
||||
|
||||
ChildElement element = new ChildElement();
|
||||
element.name = name;
|
||||
element.content.add(createGrandChildElement(name + "1"));
|
||||
element.content.add(createGrandChildElement(name + "2"));
|
||||
return element;
|
||||
}
|
||||
|
||||
private GrandChildElement createGrandChildElement(String content) {
|
||||
|
||||
GrandChildElement element = new GrandChildElement();
|
||||
element.content = content;
|
||||
return element;
|
||||
}
|
||||
|
||||
interface DummyEntityRepository extends CrudRepository<DummyEntity, Long> {
|
||||
long deleteByName(String name);
|
||||
}
|
||||
|
||||
@Configuration
|
||||
@Import(TestConfiguration.class)
|
||||
static class Config {
|
||||
|
||||
@Autowired JdbcRepositoryFactory factory;
|
||||
|
||||
@Bean
|
||||
Class<?> testClass() {
|
||||
return JdbcRepositoryWithCollectionsChainIntegrationTests.class;
|
||||
}
|
||||
|
||||
@Bean
|
||||
DummyEntityRepository dummyEntityRepository() {
|
||||
return factory.getRepository(DummyEntityRepository.class);
|
||||
}
|
||||
}
|
||||
|
||||
@Data
|
||||
static class DummyEntity {
|
||||
|
||||
String name;
|
||||
Set<ChildElement> content = new HashSet<>();
|
||||
@Id private Long id;
|
||||
|
||||
}
|
||||
|
||||
@RequiredArgsConstructor
|
||||
static class ChildElement {
|
||||
|
||||
String name;
|
||||
Set<GrandChildElement> content = new HashSet<>();
|
||||
@Id private Long id;
|
||||
}
|
||||
|
||||
@RequiredArgsConstructor
|
||||
static class GrandChildElement {
|
||||
|
||||
String content;
|
||||
@Id private Long id;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -173,6 +173,26 @@ public class JdbcRepositoryWithCollectionsIntegrationTests {
|
||||
assertThat(count).isEqualTo(0);
|
||||
}
|
||||
|
||||
@Test // DATAJDBC-551
|
||||
public void deleteByName() {
|
||||
|
||||
Element element1 = createElement("one");
|
||||
Element element2 = createElement("two");
|
||||
|
||||
DummyEntity entity = createDummyEntity();
|
||||
entity.content.add(element1);
|
||||
entity.content.add(element2);
|
||||
|
||||
entity = repository.save(entity);
|
||||
|
||||
assertThat(repository.deleteByName("Entity Name")).isEqualTo(1);
|
||||
|
||||
assertThat(repository.findById(entity.id)).isEmpty();
|
||||
|
||||
Long count = template.queryForObject("select count(1) from Element", new HashMap<>(), Long.class);
|
||||
assertThat(count).isEqualTo(0);
|
||||
}
|
||||
|
||||
private Element createElement(String content) {
|
||||
|
||||
Element element = new Element();
|
||||
@@ -180,7 +200,9 @@ public class JdbcRepositoryWithCollectionsIntegrationTests {
|
||||
return element;
|
||||
}
|
||||
|
||||
interface DummyEntityRepository extends CrudRepository<DummyEntity, Long> {}
|
||||
interface DummyEntityRepository extends CrudRepository<DummyEntity, Long> {
|
||||
long deleteByName(String name);
|
||||
}
|
||||
|
||||
@Configuration
|
||||
@Import(TestConfiguration.class)
|
||||
|
||||
@@ -0,0 +1,3 @@
|
||||
CREATE TABLE dummy_entity ( id BIGINT GENERATED BY DEFAULT AS IDENTITY ( START WITH 1 ) PRIMARY KEY, NAME VARCHAR(100));
|
||||
CREATE TABLE child_element (id BIGINT GENERATED BY DEFAULT AS IDENTITY (START WITH 1) PRIMARY KEY, NAME VARCHAR(100), dummy_entity BIGINT);
|
||||
CREATE TABLE grand_child_element (id BIGINT GENERATED BY DEFAULT AS IDENTITY (START WITH 1) PRIMARY KEY, CONTENT VARCHAR(100), child_element BIGINT);
|
||||
Reference in New Issue
Block a user