Skip to content

Commit e88d786

Browse files
committed
early returns
1 parent 453c102 commit e88d786

File tree

3 files changed

+45
-50
lines changed

3 files changed

+45
-50
lines changed

spring-data-jdbc/src/main/java/org/springframework/data/jdbc/core/mapping/IdGeneratingBeforeSaveCallback.java

Lines changed: 20 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -54,26 +54,7 @@ public Object onBeforeSave(Object aggregate, MutableAggregateChange<Object> aggr
5454
.getRequiredPersistentEntity(aggregate.getClass());
5555
Optional<SqlIdentifier> idSequence = persistentEntity.getIdSequence();
5656

57-
if (dialect.getIdGeneration().sequencesSupported()) {
58-
59-
if (persistentEntity.hasIdProperty()) {
60-
61-
PersistentPropertyAccessor<Object> accessor = persistentEntity.getPropertyAccessor(aggregate);
62-
63-
idSequence.map(this::querySequence).ifPresent(idValue -> {
64-
RelationalPersistentProperty idProperty = persistentEntity.getRequiredIdProperty();
65-
if (idProperty.isEmbedded()) {
66-
67-
setEmbeddedIdValue(persistentEntity, idProperty, aggregate, idValue, accessor);
68-
69-
} else {
70-
accessor.setProperty(idProperty, idValue);
71-
}
72-
73-
});
74-
return accessor.getBean();
75-
}
76-
} else {
57+
if (!dialect.getIdGeneration().sequencesSupported()) {
7758
if (idSequence.isPresent()) {
7859
LOG.warn(
7960
"""
@@ -82,9 +63,27 @@ public Object onBeforeSave(Object aggregate, MutableAggregateChange<Object> aggr
8263
"""
8364
.formatted(aggregate.getClass().getName()));
8465
}
66+
return aggregate;
8567
}
8668

87-
return aggregate;
69+
if (!persistentEntity.hasIdProperty()) {
70+
return aggregate;
71+
}
72+
73+
PersistentPropertyAccessor<Object> accessor = persistentEntity.getPropertyAccessor(aggregate);
74+
75+
idSequence.map(this::querySequence).ifPresent(idValue -> {
76+
RelationalPersistentProperty idProperty = persistentEntity.getRequiredIdProperty();
77+
if (idProperty.isEmbedded()) {
78+
79+
setEmbeddedIdValue(persistentEntity, idProperty, aggregate, idValue, accessor);
80+
81+
} else {
82+
accessor.setProperty(idProperty, idValue);
83+
}
84+
85+
});
86+
return accessor.getBean();
8887
}
8988

9089
private void setEmbeddedIdValue(RelationalPersistentEntity<?> persistentEntity,

spring-data-jdbc/src/test/java/org/springframework/data/jdbc/core/convert/SqlGeneratorEmbeddedUnitTests.java

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@
3434
import org.springframework.data.relational.core.mapping.RelationalPersistentProperty;
3535
import org.springframework.data.relational.core.mapping.Table;
3636
import org.springframework.data.relational.core.sql.Aliased;
37-
import org.springframework.data.relational.core.sql.Conditions;
3837
import org.springframework.data.relational.core.sql.SqlIdentifier;
3938
import org.springframework.lang.Nullable;
4039

@@ -446,18 +445,8 @@ void joinForEmbeddedWithReference() {
446445

447446
assertSoftly(softly -> {
448447
softly.assertThat(join.joinTable().getName()).isEqualTo(SqlIdentifier.unquoted("other_entity"));
449-
// softly.assertThat(join.columns()).extracting( //
450-
// pair -> pair.getFirst().getTable(), //
451-
// pair -> pair.getFirst().getName(), //
452-
// pair -> pair.getSecond().getTable().getName(), //
453-
// pair -> pair.getSecond().getName() //
454-
// ).contains(tuple( //
455-
// join.joinTable(), //
456-
// SqlIdentifier.unquoted("dummy_entity2"), //
457-
// SqlIdentifier.unquoted("dummy_entity2"), //
458-
// SqlIdentifier.unquoted("id") //
459-
// ));
460-
softly.assertThat(join.condition()).isEqualTo(org.springframework.data.relational.core.sql.Column.create("dummy_entity2", join.joinTable()).isEqualTo(org.springframework.data.relational.core.sql.Column.create("id", org.springframework.data.relational.core.sql.Table.create("dummy_entity2"))));
448+
softly.assertThat(join.condition())
449+
.isEqualTo(SqlGeneratorUnitTests.equalsCondition("dummy_entity2", "id", join.joinTable(), "dummy_entity2"));
461450
});
462451
}
463452

spring-data-jdbc/src/test/java/org/springframework/data/jdbc/core/convert/SqlGeneratorUnitTests.java

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
import org.springframework.data.relational.core.query.Criteria;
5151
import org.springframework.data.relational.core.query.Query;
5252
import org.springframework.data.relational.core.sql.Aliased;
53+
import org.springframework.data.relational.core.sql.Comparison;
5354
import org.springframework.data.relational.core.sql.LockMode;
5455
import org.springframework.data.relational.core.sql.SqlIdentifier;
5556
import org.springframework.data.relational.core.sql.Table;
@@ -85,6 +86,22 @@ class SqlGeneratorUnitTests {
8586
});
8687
private SqlGenerator sqlGenerator;
8788

89+
static Comparison equalsCondition(Table parentTable, SqlIdentifier parentId, Table joinedTable,
90+
SqlIdentifier joinedColumn) {
91+
return org.springframework.data.relational.core.sql.Column.create(joinedColumn, joinedTable)
92+
.isEqualTo(org.springframework.data.relational.core.sql.Column.create(parentId, parentTable));
93+
}
94+
95+
static Comparison equalsCondition(SqlIdentifier parentTable, SqlIdentifier parentId, Table joinedTable,
96+
SqlIdentifier joinedColumn) {
97+
return equalsCondition(Table.create(parentTable), parentId, joinedTable, joinedColumn);
98+
}
99+
100+
static Comparison equalsCondition(String parentTable, String parentId, Table joinedTable, String joinedColumn) {
101+
return equalsCondition(SqlIdentifier.unquoted(parentTable), SqlIdentifier.unquoted(parentId), joinedTable,
102+
SqlIdentifier.unquoted(joinedColumn));
103+
}
104+
88105
@BeforeEach
89106
void setUp() {
90107
this.sqlGenerator = createSqlGenerator(DummyEntity.class);
@@ -716,10 +733,8 @@ void joinForSimpleReference() {
716733
assertSoftly(softly -> {
717734

718735
softly.assertThat(join.joinTable().getName()).isEqualTo(SqlIdentifier.quoted("REFERENCED_ENTITY"));
719-
softly.assertThat(join.condition())
720-
.isEqualTo(org.springframework.data.relational.core.sql.Column.create(SqlIdentifier.quoted("DUMMY_ENTITY"), join.joinTable())
721-
.isEqualTo(org.springframework.data.relational.core.sql.Column.create(SqlIdentifier.quoted("id1"),
722-
org.springframework.data.relational.core.sql.Table.create(SqlIdentifier.quoted("DUMMY_ENTITY")))));
736+
softly.assertThat(join.condition()).isEqualTo(equalsCondition(SqlIdentifier.quoted("DUMMY_ENTITY"),
737+
SqlIdentifier.quoted("id1"), join.joinTable(), SqlIdentifier.quoted("DUMMY_ENTITY")));
723738

724739
});
725740
}
@@ -749,11 +764,8 @@ void joinForSecondLevelReference() {
749764
assertSoftly(softly -> {
750765
softly.assertThat(join.joinTable().getName()).isEqualTo(SqlIdentifier.quoted("SECOND_LEVEL_REFERENCED_ENTITY"));
751766
softly.assertThat(join.condition())
752-
.isEqualTo(org.springframework.data.relational.core.sql.Column
753-
.create(SqlIdentifier.quoted("REFERENCED_ENTITY"), join.joinTable())
754-
.isEqualTo(org.springframework.data.relational.core.sql.Column.create(SqlIdentifier.quoted("X_L1ID"),
755-
org.springframework.data.relational.core.sql.Table.create("REFERENCED_ENTITY")
756-
.as(SqlIdentifier.quoted("ref")))));
767+
.isEqualTo(equalsCondition(Table.create("REFERENCED_ENTITY").as(SqlIdentifier.quoted("ref")),
768+
SqlIdentifier.quoted("X_L1ID"), join.joinTable(), SqlIdentifier.quoted("REFERENCED_ENTITY")));
757769

758770
});
759771
}
@@ -769,13 +781,8 @@ void joinForOneToOneWithoutId() {
769781
softly.assertThat(joinTable.getName()).isEqualTo(SqlIdentifier.quoted("NO_ID_CHILD"));
770782
softly.assertThat(joinTable).isInstanceOf(Aliased.class);
771783
softly.assertThat(((Aliased) joinTable).getAlias()).isEqualTo(SqlIdentifier.quoted("child"));
772-
softly.assertThat(join.condition())
773-
.isEqualTo(org.springframework.data.relational.core.sql.Column
774-
.create(SqlIdentifier.quoted("PARENT_OF_NO_ID_CHILD"), join.joinTable())
775-
.isEqualTo(org.springframework.data.relational.core.sql.Column.create(SqlIdentifier.quoted("X_ID"),
776-
org.springframework.data.relational.core.sql.Table
777-
.create(SqlIdentifier.quoted("PARENT_OF_NO_ID_CHILD")))));
778-
784+
softly.assertThat(join.condition()).isEqualTo(equalsCondition(SqlIdentifier.quoted("PARENT_OF_NO_ID_CHILD"),
785+
SqlIdentifier.quoted("X_ID"), join.joinTable(), SqlIdentifier.quoted("PARENT_OF_NO_ID_CHILD")));
779786
});
780787
}
781788

0 commit comments

Comments
 (0)