Skip to content

Issue1502/root whitout id exception #1843

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.function.Function;
import java.util.stream.Collectors;
Expand Down Expand Up @@ -67,6 +68,7 @@
* @author Myeonghyeon Lee
* @author Chirag Tailor
* @author Diego Krupitza
* @author Andrey Rosa
*/
public class JdbcAggregateTemplate implements JdbcAggregateOperations {

Expand Down Expand Up @@ -153,8 +155,8 @@ public void setEntityCallbacks(EntityCallbacks entityCallbacks) {
* published or whether emission should be suppressed. Enabled by default.
*
* @param enabled {@code true} to enable entity lifecycle events; {@code false} to disable entity lifecycle events.
* @since 3.0
* @see AbstractRelationalEvent
* @since 3.0
*/
public void setEntityLifecycleEventsEnabled(boolean enabled) {
this.eventDelegate.setEventsEnabled(enabled);
Expand Down Expand Up @@ -447,11 +449,6 @@ private <T> void doDeleteAll(Iterable<? extends T> instances, Class<T> domainTyp

private <T> T afterExecute(AggregateChange<T> change, T entityAfterExecution) {

Object identifier = context.getRequiredPersistentEntity(change.getEntityType())
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't see the need to extract the check out of this method.

.getIdentifierAccessor(entityAfterExecution).getIdentifier();

Assert.notNull(identifier, "After saving the identifier must not be null");

return triggerAfterSave(entityAfterExecution, change);
}

Expand Down Expand Up @@ -482,7 +479,6 @@ private <T> void deleteTree(Object id, @Nullable T entity, Class<T> domainType)
}

private <T> T performSave(EntityAndChangeCreator<T> instance) {

// noinspection unchecked
BatchingAggregateChange<T, RootAggregateChange<T>> batchingAggregateChange = //
BatchingAggregateChange.forSave((Class<T>) ClassUtils.getUserClass(instance.entity));
Expand All @@ -492,7 +488,10 @@ private <T> T performSave(EntityAndChangeCreator<T> instance) {

Assert.isTrue(afterExecutionIterator.hasNext(), "Instances after execution must not be empty");

return afterExecute(batchingAggregateChange, afterExecutionIterator.next());
T afterExecute = afterExecutionIterator.next();
checkIfHaveId(context.getRequiredPersistentEntity(batchingAggregateChange.getEntityType())
.getIdentifierAccessor(afterExecute));
return afterExecute(batchingAggregateChange, afterExecute);
}

private <T> List<T> performSaveAll(Iterable<EntityAndChangeCreator<T>> instances) {
Expand All @@ -513,12 +512,22 @@ private <T> List<T> performSaveAll(Iterable<EntityAndChangeCreator<T>> instances

ArrayList<T> results = new ArrayList<>(instancesAfterExecution.size());
for (T instance : instancesAfterExecution) {
results.add(afterExecute(batchingAggregateChange, instance));
T afterExecuteElement = afterExecute(batchingAggregateChange, instance);
checkIfHaveId(context.getRequiredPersistentEntity(batchingAggregateChange.getEntityType())
.getIdentifierAccessor(afterExecuteElement));
results.add(afterExecuteElement);
}

return results;
}

private <T> void checkIfHaveId(IdentifierAccessor context) {
Object identifier = context.getIdentifier();
if (Objects.isNull(identifier)) {
throw new IllegalStateException("After saving the identifier must not be null");
}
}

private <T> Function<T, RootAggregateChange<T>> changeCreatorSelectorForSave(T instance) {

return context.getRequiredPersistentEntity(instance.getClass()).isNew(instance)
Expand Down Expand Up @@ -656,9 +665,9 @@ private <T> T triggerBeforeDelete(@Nullable T aggregateRoot, Object id, MutableA
return null;
}

private record EntityAndPreviousVersion<T> (T entity, @Nullable Number version) {
private record EntityAndPreviousVersion<T>(T entity, @Nullable Number version) {
}

private record EntityAndChangeCreator<T> (T entity, Function<T, RootAggregateChange<T>> changeCreator) {
private record EntityAndChangeCreator<T>(T entity, Function<T, RootAggregateChange<T>> changeCreator) {
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
import static java.util.Arrays.*;
import static java.util.Collections.*;
import static org.assertj.core.api.Assertions.*;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.Mockito.*;

import org.junit.jupiter.api.BeforeEach;
Expand Down Expand Up @@ -53,6 +55,7 @@
* @author Mark Paluch
* @author Milan Milanov
* @author Chirag Tailor
* @author Andrey Rosa
*/
@ExtendWith(MockitoExtension.class)
public class JdbcAggregateTemplateUnitTests {
Expand Down Expand Up @@ -161,6 +164,18 @@ void savePreparesInstanceWithInitialVersion_onInsert_whenVersionPropertyIsImmuta
assertThat(afterConvert.getVersion()).isEqualTo(0L);
}

@Test
// GH-1502
void savePreparesInstanceWithoutIdentifier() {

EntityWitoutId entity = new EntityWitoutId();
when(callbacks.callback(any(), any(), any(Object[].class))).thenReturn(entity, entity);

IllegalStateException exception = assertThrows(IllegalStateException.class, () -> {
template.save(entity);
});
assertEquals("After saving the identifier must not be null", exception.getMessage());
}
@Test
// GH-1137
void savePreparesInstanceWithInitialVersion_onInsert_whenVersionPropertyIsPrimitiveType() {
Expand Down Expand Up @@ -423,7 +438,26 @@ public Long getVersion() {
return this.version;
}
}
private static class EntityWitoutId {


@Version
private long version;

public EntityWitoutId() {

}



public long getVersion() {
return this.version;
}

public void setVersion(long version) {
this.version = version;
}
}
private static class EntityWithPrimitiveVersion {

@Column("id1")
Expand Down
Loading