Polishing.

Formatting.
Fixing SQL scripts.

Original pull request #1956
See #831
This commit is contained in:
Jens Schauder
2025-03-17 14:42:28 +01:00
committed by Mark Paluch
parent 4e465972fb
commit 8225d692f6
12 changed files with 131 additions and 151 deletions

View File

@@ -78,6 +78,7 @@ class AggregateChangeExecutor {
}
private void execute(DbAction<?> action, JdbcAggregateChangeExecutionContext executionContext) {
if (action instanceof DbAction.InsertRoot<?> insertRoot) {
executionContext.executeInsertRoot(insertRoot);
} else if (action instanceof DbAction.BatchInsertRoot<?> batchInsertRoot) {

View File

@@ -53,7 +53,6 @@ import org.springframework.data.jdbc.testing.TestClass;
import org.springframework.data.jdbc.testing.TestConfiguration;
import org.springframework.data.jdbc.testing.TestDatabaseFeatures;
import org.springframework.data.mapping.context.InvalidPersistentPropertyPath;
import org.springframework.data.relational.core.conversion.DbActionExecutionException;
import org.springframework.data.relational.core.mapping.Column;
import org.springframework.data.relational.core.mapping.Embedded;
import org.springframework.data.relational.core.mapping.InsertOnlyProperty;
@@ -317,7 +316,7 @@ abstract class AbstractJdbcAggregateTemplateIntegrationTests {
Stream<LegoSet> streamable = template.streamAll(LegoSet.class);
assertThat(streamable)
assertThat(streamable) //
.extracting("id", "manual.id", "manual.content") //
.containsExactly(tuple(legoSet.id, legoSet.manual.id, legoSet.manual.content));
}
@@ -730,7 +729,7 @@ abstract class AbstractJdbcAggregateTemplateIntegrationTests {
void saveAndLoadAnEntityWithEmptyArray() {
ArrayOwner arrayOwner = new ArrayOwner();
arrayOwner.digits = new String[] { };
arrayOwner.digits = new String[] {};
ArrayOwner saved = template.save(arrayOwner);
@@ -854,7 +853,7 @@ abstract class AbstractJdbcAggregateTemplateIntegrationTests {
assertThat(reloaded.digits).isEqualTo(new HashSet<>(asList("one", "two", "three")));
}
@Test //GH-1737
@Test // GH-1737
@EnabledOnFeature(SUPPORTS_ARRAYS)
void saveAndLoadEmbeddedArray() {
@@ -869,7 +868,7 @@ abstract class AbstractJdbcAggregateTemplateIntegrationTests {
assertThat(reloaded.embeddedStringList.digits).containsExactly("one", "two", "three");
}
@Test //GH-1737
@Test // GH-1737
@EnabledOnFeature(SUPPORTS_ARRAYS)
void saveAndLoadEmptyEmbeddedArray() {

View File

@@ -73,12 +73,9 @@ public class JdbcRepositoryConcurrencyIntegrationTests {
}
}
@Autowired
NamedParameterJdbcTemplate template;
@Autowired
DummyEntityRepository repository;
@Autowired
PlatformTransactionManager transactionManager;
@Autowired NamedParameterJdbcTemplate template;
@Autowired DummyEntityRepository repository;
@Autowired PlatformTransactionManager transactionManager;
List<DummyEntity> concurrencyEntities;
DummyEntity entity;
@@ -216,7 +213,7 @@ public class JdbcRepositoryConcurrencyIntegrationTests {
}
private void executeInParallel(CountDownLatch startLatch, CountDownLatch doneLatch,
UnaryOperator<DummyEntity> deleteAction, DummyEntity entity) {
UnaryOperator<DummyEntity> deleteAction, DummyEntity entity) {
// delete
new Thread(() -> {
try {
@@ -253,13 +250,11 @@ public class JdbcRepositoryConcurrencyIntegrationTests {
return new DummyEntity(null, "Entity Name", new ArrayList<>());
}
interface DummyEntityRepository extends CrudRepository<DummyEntity, Long> {
}
interface DummyEntityRepository extends CrudRepository<DummyEntity, Long> {}
static class DummyEntity {
@Id
private Long id;
@Id private Long id;
String name;
final List<Element> content;
@@ -292,8 +287,7 @@ public class JdbcRepositoryConcurrencyIntegrationTests {
static class Element {
@Id
private Long id;
@Id private Long id;
final Long content;
public Element(Long id, Long content) {

View File

@@ -15,12 +15,10 @@
*/
package org.springframework.data.jdbc.repository;
import static java.util.Arrays.asList;
import static java.util.Collections.emptyList;
import static java.util.Collections.singletonList;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.assertj.core.api.SoftAssertions.assertSoftly;
import static java.util.Arrays.*;
import static java.util.Collections.*;
import static org.assertj.core.api.Assertions.*;
import static org.assertj.core.api.SoftAssertions.*;
import java.io.IOException;
import java.sql.ResultSet;
@@ -39,7 +37,6 @@ import java.util.Set;
import java.util.function.Consumer;
import java.util.stream.Stream;
import org.assertj.core.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
@@ -56,17 +53,7 @@ import org.springframework.dao.DuplicateKeyException;
import org.springframework.dao.IncorrectResultSizeDataAccessException;
import org.springframework.data.annotation.Id;
import org.springframework.data.annotation.Transient;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.ExampleMatcher;
import org.springframework.data.domain.Limit;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Persistable;
import org.springframework.data.domain.ScrollPosition;
import org.springframework.data.domain.Slice;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Window;
import org.springframework.data.domain.*;
import org.springframework.data.jdbc.core.mapping.AggregateReference;
import org.springframework.data.jdbc.repository.query.Modifying;
import org.springframework.data.jdbc.repository.query.Query;
@@ -126,6 +113,37 @@ public class JdbcRepositoryIntegrationTests {
@Autowired WithDelimitedColumnRepository withDelimitedColumnRepository;
@Autowired EntityWithSequenceRepository entityWithSequenceRepository;
public static Stream<Arguments> findAllByExamplePageableSource() {
return Stream.of( //
Arguments.of(PageRequest.of(0, 3), 3, 34, Arrays.asList("3", "4", "100")), //
Arguments.of(PageRequest.of(1, 10), 10, 10, Arrays.asList("9", "20", "30")), //
Arguments.of(PageRequest.of(2, 10), 10, 10, Arrays.asList("1", "2", "3")), //
Arguments.of(PageRequest.of(33, 3), 1, 34, Collections.emptyList()), //
Arguments.of(PageRequest.of(36, 3), 0, 34, Collections.emptyList()), //
Arguments.of(PageRequest.of(0, 10000), 100, 1, Collections.emptyList()), //
Arguments.of(PageRequest.of(100, 10000), 0, 1, Collections.emptyList()) //
);
}
private static DummyEntity createEntity() {
return createEntity("Entity Name");
}
private static DummyEntity createEntity(String entityName) {
return createEntity(entityName, it -> {});
}
private static DummyEntity createEntity(String entityName, Consumer<DummyEntity> customizer) {
DummyEntity entity = new DummyEntity();
entity.setName(entityName);
customizer.accept(entity);
return entity;
}
@BeforeEach
public void before() {
@@ -225,16 +243,14 @@ public class JdbcRepositoryIntegrationTests {
.containsExactlyInAnyOrder(entity.getIdProp(), other.getIdProp());
}
@Test // DATAJDBC-611
public void testDuplicateKeyExceptionIsThrownInCaseOfUniqueKeyViolation() {
@Test // GH-831
public void duplicateKeyExceptionIsThrownInCaseOfUniqueKeyViolation() {
// given.
ProvidedIdEntity first = ProvidedIdEntity.newInstance(1L, "name");
ProvidedIdEntity second = ProvidedIdEntity.newInstance(1L, "other");
// when/then
Assertions.assertThatCode(() -> providedIdEntityRepository.save(first)).doesNotThrowAnyException();
Assertions.assertThatThrownBy(() -> providedIdEntityRepository.save(second)).isInstanceOf(DuplicateKeyException.class);
assertThatCode(() -> providedIdEntityRepository.save(first)).doesNotThrowAnyException();
assertThatThrownBy(() -> providedIdEntityRepository.save(second)).isInstanceOf(DuplicateKeyException.class);
}
@Test // DATAJDBC-97
@@ -967,18 +983,6 @@ public class JdbcRepositoryIntegrationTests {
}
}
public static Stream<Arguments> findAllByExamplePageableSource() {
return Stream.of( //
Arguments.of(PageRequest.of(0, 3), 3, 34, Arrays.asList("3", "4", "100")), //
Arguments.of(PageRequest.of(1, 10), 10, 10, Arrays.asList("9", "20", "30")), //
Arguments.of(PageRequest.of(2, 10), 10, 10, Arrays.asList("1", "2", "3")), //
Arguments.of(PageRequest.of(33, 3), 1, 34, Collections.emptyList()), //
Arguments.of(PageRequest.of(36, 3), 0, 34, Collections.emptyList()), //
Arguments.of(PageRequest.of(0, 10000), 100, 1, Collections.emptyList()), //
Arguments.of(PageRequest.of(100, 10000), 0, 1, Collections.emptyList()) //
);
}
@Test // GH-1192
void existsByExampleShouldGetOne() {
@@ -1461,6 +1465,10 @@ public class JdbcRepositoryIntegrationTests {
return now;
}
enum Direction {
LEFT, CENTER, RIGHT
}
interface DummyProjectExample {
String getName();
}
@@ -1565,6 +1573,10 @@ public class JdbcRepositoryIntegrationTests {
interface EntityWithSequenceRepository extends CrudRepository<EntityWithSequence, Long> {}
interface DummyProjection {
String getName();
}
@Configuration
@Import(TestConfiguration.class)
static class Config {
@@ -1709,22 +1721,22 @@ public class JdbcRepositoryIntegrationTests {
return this.id;
}
public String getIdentifier() {
return this.identifier;
}
public String getType() {
return this.type;
}
public void setId(Long id) {
this.id = id;
}
public String getIdentifier() {
return this.identifier;
}
public void setIdentifier(String identifier) {
this.identifier = identifier;
}
public String getType() {
return this.type;
}
public void setType(String type) {
this.type = type;
}
@@ -1866,6 +1878,11 @@ public class JdbcRepositoryIntegrationTests {
return "myext";
}
@Override
public Object getRootObject() {
return new ExtensionRoot();
}
public static class ExtensionRoot {
// just public for testing purposes
public static Long ID = 1L;
@@ -1874,29 +1891,6 @@ public class JdbcRepositoryIntegrationTests {
return ID;
}
}
@Override
public Object getRootObject() {
return new ExtensionRoot();
}
}
private static DummyEntity createEntity() {
return createEntity("Entity Name");
}
private static DummyEntity createEntity(String entityName) {
return createEntity(entityName, it -> {});
}
private static DummyEntity createEntity(String entityName, Consumer<DummyEntity> customizer) {
DummyEntity entity = new DummyEntity();
entity.setName(entityName);
customizer.accept(entity);
return entity;
}
static class EntityWithSequence {
@@ -1926,13 +1920,11 @@ public class JdbcRepositoryIntegrationTests {
static class ProvidedIdEntity implements Persistable<Long> {
@Id
private final Long id;
@Id private final Long id;
private String name;
@Transient
private boolean isNew;
@Transient private boolean isNew;
private ProvidedIdEntity(Long id, String name, boolean isNew) {
this.id = id;
@@ -1960,11 +1952,11 @@ public class JdbcRepositoryIntegrationTests {
String name;
Instant pointInTime;
OffsetDateTime offsetDateTime;
@Id private Long idProp;
boolean flag;
AggregateReference<DummyEntity, Long> ref;
Direction direction;
byte[] bytes = new byte[] { 0, 0, 0, 0, 0, 0, 0, 0 };
@Id private Long idProp;
public DummyEntity(String name) {
this.name = name;
@@ -1976,54 +1968,54 @@ public class JdbcRepositoryIntegrationTests {
return this.name;
}
public Instant getPointInTime() {
return this.pointInTime;
}
public OffsetDateTime getOffsetDateTime() {
return this.offsetDateTime;
}
public Long getIdProp() {
return this.idProp;
}
public boolean isFlag() {
return this.flag;
}
public AggregateReference<DummyEntity, Long> getRef() {
return this.ref;
}
public Direction getDirection() {
return this.direction;
}
public void setName(String name) {
this.name = name;
}
public Instant getPointInTime() {
return this.pointInTime;
}
public void setPointInTime(Instant pointInTime) {
this.pointInTime = pointInTime;
}
public OffsetDateTime getOffsetDateTime() {
return this.offsetDateTime;
}
public void setOffsetDateTime(OffsetDateTime offsetDateTime) {
this.offsetDateTime = offsetDateTime;
}
public Long getIdProp() {
return this.idProp;
}
public void setIdProp(Long idProp) {
this.idProp = idProp;
}
public boolean isFlag() {
return this.flag;
}
public void setFlag(boolean flag) {
this.flag = flag;
}
public AggregateReference<DummyEntity, Long> getRef() {
return this.ref;
}
public void setRef(AggregateReference<DummyEntity, Long> ref) {
this.ref = ref;
}
public Direction getDirection() {
return this.direction;
}
public void setDirection(Direction direction) {
this.direction = direction;
}
@@ -2045,24 +2037,20 @@ public class JdbcRepositoryIntegrationTests {
return Objects.hash(name, pointInTime, offsetDateTime, idProp, flag, ref, direction);
}
public void setBytes(byte[] bytes) {
this.bytes = bytes;
}
public byte[] getBytes() {
return bytes;
}
public void setBytes(byte[] bytes) {
this.bytes = bytes;
}
@Override
public String toString() {
return "DummyEntity{" + "name='" + name + '\'' + ", idProp=" + idProp + '}';
}
}
enum Direction {
LEFT, CENTER, RIGHT
}
static class DummyDto {
@Id Long idProp;
String name;
@@ -2099,10 +2087,6 @@ public class JdbcRepositoryIntegrationTests {
}
}
interface DummyProjection {
String getName();
}
static final class DtoProjection {
private final String name;

View File

@@ -52,12 +52,14 @@ CREATE TABLE WITH_DELIMITED_COLUMN
CREATE TABLE ENTITY_WITH_SEQUENCE
(
ID BIGINT,
ID BIGINT,
NAME VARCHAR(100)
);
CREATE SEQUENCE ENTITY_SEQUENCE START WITH 1 INCREMENT BY 1 NO MAXVALUE;
CREATE TABLE PROVIDED_ID_ENTITY
(
ID BIGINT PRIMARY KEY,
NAME VARCHAR(30)
ID BIGINT NOT NULL PRIMARY KEY,
NAME VARCHAR(30)
);

View File

@@ -43,7 +43,7 @@ CREATE TABLE WITH_DELIMITED_COLUMN
CREATE TABLE ENTITY_WITH_SEQUENCE
(
ID BIGINT,
ID BIGINT,
NAME VARCHAR(100)
);
@@ -51,6 +51,6 @@ CREATE SEQUENCE ENTITY_SEQUENCE START WITH 1 INCREMENT BY 1 NO MAXVALUE;
CREATE TABLE PROVIDED_ID_ENTITY
(
ID BIGINT PRIMARY KEY,
NAME VARCHAR(30)
ID BIGINT PRIMARY KEY,
NAME VARCHAR(30)
);

View File

@@ -43,7 +43,7 @@ CREATE TABLE WITH_DELIMITED_COLUMN
CREATE TABLE ENTITY_WITH_SEQUENCE
(
ID BIGINT,
ID BIGINT,
NAME VARCHAR(100)
);
@@ -51,6 +51,6 @@ CREATE SEQUENCE ENTITY_SEQUENCE START WITH 1 INCREMENT BY 1 NO MAXVALUE;
CREATE TABLE PROVIDED_ID_ENTITY
(
ID BIGINT PRIMARY KEY,
NAME VARCHAR(30)
ID BIGINT PRIMARY KEY,
NAME VARCHAR(30)
);

View File

@@ -43,7 +43,7 @@ CREATE TABLE WITH_DELIMITED_COLUMN
CREATE TABLE ENTITY_WITH_SEQUENCE
(
ID BIGINT,
ID BIGINT,
NAME VARCHAR(100)
);
@@ -51,6 +51,6 @@ CREATE SEQUENCE `ENTITY_SEQUENCE` START WITH 1 INCREMENT BY 1 NO MAXVALUE;
CREATE TABLE PROVIDED_ID_ENTITY
(
ID BIGINT PRIMARY KEY,
NAME VARCHAR(30)
ID BIGINT PRIMARY KEY,
NAME VARCHAR(30)
);

View File

@@ -52,7 +52,7 @@ CREATE TABLE WITH_DELIMITED_COLUMN
CREATE TABLE ENTITY_WITH_SEQUENCE
(
ID BIGINT,
ID BIGINT,
NAME VARCHAR(100)
);
@@ -60,6 +60,6 @@ CREATE SEQUENCE ENTITY_SEQUENCE START WITH 1 INCREMENT BY 1 NO MAXVALUE;
CREATE TABLE PROVIDED_ID_ENTITY
(
ID BIGINT PRIMARY KEY,
NAME VARCHAR(30)
ID BIGINT PRIMARY KEY,
NAME VARCHAR(30)
);

View File

@@ -1,5 +1,5 @@
SET
SQL_MODE='ALLOW_INVALID_DATES';
SQL_MODE = 'ALLOW_INVALID_DATES';
CREATE TABLE DUMMY_ENTITY
(
@@ -46,6 +46,6 @@ CREATE TABLE WITH_DELIMITED_COLUMN
CREATE TABLE PROVIDED_ID_ENTITY
(
ID BIGINT PRIMARY KEY,
NAME VARCHAR(30)
ID BIGINT PRIMARY KEY,
NAME VARCHAR(30)
);

View File

@@ -52,14 +52,14 @@ CREATE TABLE WITH_DELIMITED_COLUMN
CREATE TABLE ENTITY_WITH_SEQUENCE
(
ID NUMBER,
NAME VARCHAR(100)
ID NUMBER,
NAME VARCHAR2(100)
);
CREATE SEQUENCE ENTITY_SEQUENCE START WITH 1 INCREMENT BY 1;
CREATE TABLE PROVIDED_ID_ENTITY
(
ID BIGINT PRIMARY KEY,
NAME VARCHAR(30)
ID NUMBER PRIMARY KEY,
NAME VARCHAR2(30)
);

View File

@@ -52,7 +52,7 @@ CREATE TABLE "WITH_DELIMITED_COLUMN"
CREATE TABLE ENTITY_WITH_SEQUENCE
(
ID BIGINT,
ID BIGINT,
NAME VARCHAR(100)
);
@@ -60,6 +60,6 @@ CREATE SEQUENCE "ENTITY_SEQUENCE" START WITH 1 INCREMENT BY 1 NO MAXVALUE;
CREATE TABLE PROVIDED_ID_ENTITY
(
ID BIGINT PRIMARY KEY,
NAME VARCHAR(30)
ID BIGINT PRIMARY KEY,
NAME VARCHAR(30)
);