Skip to content

Commit 04bf615

Browse files
committed
Wrap associations in EmbeddedRelationalPersistentEntity.
We now also wrap associations to ensure embedded prefix propagation. Closes #1695
1 parent a40dbc9 commit 04bf615

File tree

2 files changed

+27
-2
lines changed

2 files changed

+27
-2
lines changed

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

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,14 @@
2424
import org.junit.jupiter.api.Test;
2525
import org.springframework.data.annotation.Id;
2626
import org.springframework.data.jdbc.core.PersistentPropertyPathTestUtils;
27+
import org.springframework.data.jdbc.core.mapping.AggregateReference;
2728
import org.springframework.data.jdbc.core.mapping.JdbcMappingContext;
2829
import org.springframework.data.relational.core.mapping.Column;
2930
import org.springframework.data.relational.core.mapping.Embedded;
3031
import org.springframework.data.relational.core.mapping.Embedded.OnEmpty;
3132
import org.springframework.data.relational.core.mapping.RelationalMappingContext;
3233
import org.springframework.data.relational.core.mapping.RelationalPersistentEntity;
34+
import org.springframework.data.relational.core.mapping.Table;
3335
import org.springframework.data.relational.core.sql.Aliased;
3436
import org.springframework.data.relational.core.sql.SqlIdentifier;
3537
import org.springframework.lang.Nullable;
@@ -38,6 +40,7 @@
3840
* Unit tests for the {@link SqlGenerator} in a context of the {@link Embedded} annotation.
3941
*
4042
* @author Bastian Wilhelm
43+
* @author Mark Paluch
4144
*/
4245
public class SqlGeneratorEmbeddedUnitTests {
4346

@@ -213,6 +216,12 @@ public void columnForEmbeddedProperty() {
213216
SqlIdentifier.unquoted("test"));
214217
}
215218

219+
@Test // GH-1695
220+
public void columnForEmbeddedPropertyWithPrefix() {
221+
assertThat(generatedColumn("nested.childId", WithEmbeddedAndAggregateReference.class))
222+
.hasToString("a.nested_child_id AS nested_child_id");
223+
}
224+
216225
@Test // DATAJDBC-340
217226
public void noColumnForEmbedded() {
218227

@@ -352,4 +361,16 @@ static class OtherEntity {
352361
String value;
353362
}
354363

364+
@Table("a")
365+
record WithEmbeddedAndAggregateReference(@Id long id,
366+
@Embedded.Nullable(prefix = "nested_") WithAggregateReference nested) {
367+
}
368+
369+
record WithAggregateReference(AggregateReference<Child, Long> childId) {
370+
}
371+
372+
record Child(@Id long id) {
373+
374+
}
375+
355376
}

spring-data-relational/src/main/java/org/springframework/data/relational/core/mapping/EmbeddedRelationalPersistentEntity.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -190,12 +190,16 @@ public void doWithProperties(SimplePropertyHandler handler) {
190190

191191
@Override
192192
public void doWithAssociations(AssociationHandler<RelationalPersistentProperty> handler) {
193-
delegate.doWithAssociations(handler);
193+
delegate.doWithAssociations((AssociationHandler<RelationalPersistentProperty>) association -> {
194+
handler.doWithAssociation(new Association<>(wrap(association.getInverse()), wrap(association.getObverse())));
195+
});
194196
}
195197

196198
@Override
197199
public void doWithAssociations(SimpleAssociationHandler handler) {
198-
delegate.doWithAssociations(handler);
200+
delegate.doWithAssociations((AssociationHandler<RelationalPersistentProperty>) association -> {
201+
handler.doWithAssociation(new Association<>(wrap(association.getInverse()), wrap(association.getObverse())));
202+
});
199203
}
200204

201205
@Override

0 commit comments

Comments
 (0)