Polishing.

Formatting and comments.

See #1201
See #1199
Original pull request #1208
This commit is contained in:
Jens Schauder
2022-03-29 09:36:06 +02:00
parent 508791b7df
commit f2dc64ec52
14 changed files with 63 additions and 28 deletions

View File

@@ -42,6 +42,15 @@ class AggregateChangeExecutor {
this.accessStrategy = accessStrategy;
}
/**
* Execute an aggregate change which has a root entity. It returns the root entity, with all changes that might apply.
* This might be the original instance or a new instance, depending on its mutability.
*
* @param aggregateChange the aggregate change to be executed. Must not be {@literal null}.
* @param <T> the type of the aggregate root.
* @return the potentially modified aggregate root. Guaranteed to be not {@literal null}.
* @since 3.0
*/
<T> T execute(AggregateChangeWithRoot<T> aggregateChange) {
JdbcAggregateChangeExecutionContext executionContext = new JdbcAggregateChangeExecutionContext(converter,
@@ -52,6 +61,13 @@ class AggregateChangeExecutor {
return executionContext.populateIdsIfNecessary();
}
/**
* Execute an aggregate change without a root entity.
*
* @param aggregateChange the aggregate change to be executed. Must not be {@literal null}.
* @param <T> the type of the aggregate root.
* @since 3.0
*/
<T> void execute(AggregateChange<T> aggregateChange) {
JdbcAggregateChangeExecutionContext executionContext = new JdbcAggregateChangeExecutionContext(converter,

View File

@@ -42,6 +42,9 @@ import org.springframework.lang.Nullable;
import org.springframework.util.Assert;
/**
* A container for the data required and produced by an aggregate change execution. Most importantly it holds the
* results of the various actions performed.
*
* @author Jens Schauder
* @author Umut Erturk
* @author Myeonghyeon Lee
@@ -226,9 +229,9 @@ class JdbcAggregateChangeExecutionContext {
DbAction.WithEntity<?> action = result.getAction();
Object newEntity = setIdAndCascadingProperties(action, result.getGeneratedId(), cascadingValues);
if (action instanceof DbAction.InsertRoot || action instanceof DbAction.UpdateRoot) {
//noinspection unchecked
// noinspection unchecked
return (T) newEntity;
}

View File

@@ -20,6 +20,9 @@ import static org.assertj.core.api.Assertions.*;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.Value;
import lombok.With;
import org.assertj.core.api.SoftAssertions;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
@@ -38,14 +41,12 @@ import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import org.springframework.transaction.annotation.Transactional;
import lombok.Value;
import lombok.With;
/**
* Integration tests for {@link JdbcAggregateTemplate} and it's handling of immutable entities.
*
* @author Jens Schauder
* @author Salim Achouche
* @author Chirag Taylor
*/
@ContextConfiguration
@Transactional
@@ -341,8 +342,7 @@ public class ImmutableAggregateTemplateHsqlIntegrationTests {
}
static class WithCopyConstructor {
@Id
private final Long id;
@Id private final Long id;
private final String name;
WithCopyConstructor(Long id, String name) {

View File

@@ -42,6 +42,12 @@ import org.springframework.data.relational.core.mapping.RelationalMappingContext
import org.springframework.data.relational.core.mapping.RelationalPersistentProperty;
import org.springframework.lang.Nullable;
/**
* Test for the {@link JdbcAggregateChangeExecutionContext} when operating on immutable classes.
*
* @author Jens Schauder
* @author Chirag Taylor
*/
public class JdbcAggregateChangeExecutorContextImmutableUnitTests {
RelationalMappingContext context = new RelationalMappingContext();

View File

@@ -19,13 +19,13 @@ import static java.util.Collections.*;
import static org.assertj.core.api.Assertions.*;
import static org.mockito.Mockito.*;
import lombok.Value;
import java.util.ArrayList;
import java.util.List;
import lombok.Value;
import org.junit.jupiter.api.Test;
import org.springframework.data.annotation.Id;
import org.springframework.data.annotation.Version;
import org.springframework.data.jdbc.core.convert.BasicJdbcConverter;
import org.springframework.data.jdbc.core.convert.DataAccessStrategy;
import org.springframework.data.jdbc.core.convert.Identifier;
@@ -174,7 +174,8 @@ public class JdbcAggregateChangeExecutorContextUnitTests {
ContentImmutableId contentImmutableId = new ContentImmutableId(null);
root.contentImmutableId = contentImmutableId;
Identifier identifier = Identifier.empty().withPart(SqlIdentifier.quoted("DUMMY_ENTITY"), 123L, Long.class);
when(accessStrategy.insert(contentImmutableId, ContentImmutableId.class, identifier, IdValueSource.GENERATED)).thenReturn(456L);
when(accessStrategy.insert(contentImmutableId, ContentImmutableId.class, identifier, IdValueSource.GENERATED))
.thenReturn(456L);
executionContext.executeInsert(createInsert(rootInsert, "contentImmutableId", contentImmutableId, null));
DummyEntity newRoot = executionContext.populateIdsIfNecessary();

View File

@@ -17,6 +17,11 @@ package org.springframework.data.jdbc.repository;
import static org.assertj.core.api.Assertions.*;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.Value;
import lombok.With;
import java.util.List;
import org.junit.jupiter.api.Test;
@@ -38,11 +43,6 @@ import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.Value;
import lombok.With;
/**
* Integration tests for the {@link BeforeSaveCallback}.
*
@@ -153,7 +153,8 @@ public class JdbcRepositoryBeforeSaveHsqlIntegrationTests {
private String name;
}
private interface MutableWithImmutableIdEntityRepository extends ListCrudRepository<MutableWithImmutableIdEntity, Long> {}
private interface MutableWithImmutableIdEntityRepository
extends ListCrudRepository<MutableWithImmutableIdEntity, Long> {}
@Data
@AllArgsConstructor
@@ -162,14 +163,14 @@ public class JdbcRepositoryBeforeSaveHsqlIntegrationTests {
private String name;
}
private interface ImmutableWithMutableIdEntityRepository extends ListCrudRepository<ImmutableWithMutableIdEntity, Long> {}
private interface ImmutableWithMutableIdEntityRepository
extends ListCrudRepository<ImmutableWithMutableIdEntity, Long> {}
@Data
@AllArgsConstructor
static class ImmutableWithMutableIdEntity {
@Id private Long id;
@With
private final String name;
@With private final String name;
}
@Configuration

View File

@@ -26,8 +26,8 @@ ALTER TABLE AUTHOR
CREATE TABLE WITH_COPY_CONSTRUCTOR
(
ID BIGINT GENERATED BY DEFAULT AS IDENTITY (START WITH 1) PRIMARY KEY,
NAME VARCHAR(30)
ID BIGINT GENERATED BY DEFAULT AS IDENTITY (START WITH 1) PRIMARY KEY,
NAME VARCHAR(30)
);
CREATE TABLE ROOT
@@ -41,4 +41,5 @@ CREATE TABLE NON_ROOT
ROOT BIGINT NOT NULL,
NAME VARCHAR(30)
);
ALTER TABLE NON_ROOT ADD FOREIGN KEY (ROOT) REFERENCES ROOT (ID);
ALTER TABLE NON_ROOT
ADD FOREIGN KEY (ROOT) REFERENCES ROOT (ID);

View File

@@ -14,8 +14,8 @@
</insert>
<select id="findById" resultType="MyBatisContext" resultMap="dummyEntityMap">
SELECT
id,
'Name based on an id' || id AS name
id,
'Name based on an id' || id AS name
FROM DummyEntity
WHERE id = #{id}
</select>

View File

@@ -14,8 +14,8 @@
</insert>
<select id="findById" resultType="MyBatisContext" resultMap="dummyEntityMap">
SELECT
id,
'name ' || id as name
id,
'name ' || id as name
FROM DummyEntity
WHERE id = #{id}
</select>

View File

@@ -209,6 +209,7 @@ public interface DbAction<T> {
* Note that deletes for contained entities that reference the root are to be represented by separate
* {@link DbAction}s.
* </p>
*
* @param <T> type of the entity for which this represents a database interaction.
*/
final class DeleteRoot<T> implements DbAction<T> {
@@ -273,6 +274,7 @@ public interface DbAction<T> {
* Note that deletes for contained entities that reference the root are to be represented by separate
* {@link DbAction}s.
* </p>
*
* @param <T> type of the entity for which this represents a database interaction.
*/
final class DeleteAllRoot<T> implements DbAction<T> {
@@ -399,6 +401,7 @@ public interface DbAction<T> {
* <p>
* Values come from parent entities but one might also add values manually.
* </p>
*
* @return guaranteed to be not {@code null}.
*/
Map<PersistentPropertyPath<RelationalPersistentProperty>, Object> getQualifiers();

View File

@@ -39,6 +39,7 @@ import org.springframework.data.relational.core.mapping.RelationalMappingContext
*
* @author Jens Schauder
* @author Myeonghyeon Lee
* @author Chirag Taylor
*/
@ExtendWith(MockitoExtension.class)
public class RelationalEntityDeleteWriterUnitTests {

View File

@@ -33,6 +33,7 @@ import org.springframework.data.relational.core.mapping.RelationalMappingContext
* Unit tests for the {@link RelationalEntityInsertWriter}
*
* @author Thomas Lang
* @author Chirag Taylor
*/
@ExtendWith(MockitoExtension.class)
public class RelationalEntityInsertWriterUnitTests {

View File

@@ -33,12 +33,13 @@ import org.springframework.data.relational.core.mapping.RelationalMappingContext
*
* @author Thomas Lang
* @author Myeonghyeon Lee
* @author Chirag Taylor
*/
@ExtendWith(MockitoExtension.class)
public class RelationalEntityUpdateWriterUnitTests {
public static final long SOME_ENTITY_ID = 23L;
private RelationalMappingContext context = new RelationalMappingContext();
private final RelationalMappingContext context = new RelationalMappingContext();
@Test // DATAJDBC-112
public void existingEntityGetsConvertedToDeletePlusUpdate() {

View File

@@ -669,7 +669,8 @@ public class RelationalEntityWriterUnitTests {
listMapContainer.maps.add(new NoIdMapContainer());
listMapContainer.maps.get(0).elements.put("one", new NoIdElement());
AggregateChangeWithRoot<NoIdListMapContainer> aggregateChange = MutableAggregateChange.forSave(listMapContainer, 1L);
AggregateChangeWithRoot<NoIdListMapContainer> aggregateChange = MutableAggregateChange.forSave(listMapContainer,
1L);
new RelationalEntityWriter<NoIdListMapContainer>(context).write(listMapContainer, aggregateChange);