|
43 | 43 | import org.springframework.data.relational.core.mapping.AggregatePath;
|
44 | 44 | import org.springframework.data.relational.core.mapping.Column;
|
45 | 45 | import org.springframework.data.relational.core.mapping.DefaultNamingStrategy;
|
| 46 | +import org.springframework.data.relational.core.mapping.MappedCollection; |
46 | 47 | import org.springframework.data.relational.core.mapping.RelationalMappingContext;
|
47 | 48 | import org.springframework.data.relational.core.mapping.RelationalPersistentEntity;
|
48 | 49 | import org.springframework.data.relational.core.mapping.RelationalPersistentProperty;
|
@@ -388,7 +389,8 @@ void findAllByPropertyWithMultipartIdentifier() {
|
388 | 389 | void findAllByPropertyWithKey() {
|
389 | 390 |
|
390 | 391 | // this would get called when ListParent is th element type of a Map
|
391 |
| - String sql = sqlGenerator.getFindAllByProperty(BACKREF, new AggregatePath.ColumnInfo(unquoted("key-column"),unquoted("key-column")), false); |
| 392 | + String sql = sqlGenerator.getFindAllByProperty(BACKREF, |
| 393 | + new AggregatePath.ColumnInfo(unquoted("key-column"), unquoted("key-column")), false); |
392 | 394 |
|
393 | 395 | assertThat(sql).isEqualTo("SELECT dummy_entity.id1 AS id1, dummy_entity.x_name AS x_name, " //
|
394 | 396 | + "dummy_entity.x_other AS x_other, " //
|
@@ -451,9 +453,9 @@ void findAllByPropertyWithEmptyBackrefColumn() {
|
451 | 453 | Identifier emptyIdentifier = Identifier.of(EMPTY, 0, Object.class);
|
452 | 454 | assertThatThrownBy(() -> sqlGenerator.getFindAllByProperty(emptyIdentifier,
|
453 | 455 | new AggregatePath.ColumnInfo(unquoted("key-column"), unquoted("key-column")), false)) //
|
454 |
| - .isInstanceOf(IllegalArgumentException.class) // |
455 |
| - .hasMessageContaining( |
456 |
| - "An empty SqlIdentifier can't be used in condition. Make sure that all composite primary keys are defined in the query"); |
| 456 | + .isInstanceOf(IllegalArgumentException.class) // |
| 457 | + .hasMessageContaining( |
| 458 | + "An empty SqlIdentifier can't be used in condition. Make sure that all composite primary keys are defined in the query"); |
457 | 459 | }
|
458 | 460 |
|
459 | 461 | @Test // DATAJDBC-219
|
@@ -625,43 +627,43 @@ void deletingLongChain() {
|
625 | 627 |
|
626 | 628 | assertThat(
|
627 | 629 | createSqlGenerator(Chain4.class).createDeleteByPath(getPath("chain3.chain2.chain1.chain0", Chain4.class))) //
|
628 |
| - .isEqualTo("DELETE FROM chain0 " + // |
629 |
| - "WHERE chain0.chain1 IN (" + // |
630 |
| - "SELECT chain1.x_one " + // |
631 |
| - "FROM chain1 " + // |
632 |
| - "WHERE chain1.chain2 IN (" + // |
633 |
| - "SELECT chain2.x_two " + // |
634 |
| - "FROM chain2 " + // |
635 |
| - "WHERE chain2.chain3 IN (" + // |
636 |
| - "SELECT chain3.x_three " + // |
637 |
| - "FROM chain3 " + // |
638 |
| - "WHERE chain3.chain4 = :rootId" + // |
639 |
| - ")))"); |
| 630 | + .isEqualTo("DELETE FROM chain0 " + // |
| 631 | + "WHERE chain0.chain1 IN (" + // |
| 632 | + "SELECT chain1.x_one " + // |
| 633 | + "FROM chain1 " + // |
| 634 | + "WHERE chain1.chain2 IN (" + // |
| 635 | + "SELECT chain2.x_two " + // |
| 636 | + "FROM chain2 " + // |
| 637 | + "WHERE chain2.chain3 IN (" + // |
| 638 | + "SELECT chain3.x_three " + // |
| 639 | + "FROM chain3 " + // |
| 640 | + "WHERE chain3.chain4 = :rootId" + // |
| 641 | + ")))"); |
640 | 642 | }
|
641 | 643 |
|
642 | 644 | @Test // DATAJDBC-359
|
643 | 645 | void deletingLongChainNoId() {
|
644 | 646 |
|
645 | 647 | assertThat(createSqlGenerator(NoIdChain4.class)
|
646 | 648 | .createDeleteByPath(getPath("chain3.chain2.chain1.chain0", NoIdChain4.class))) //
|
647 |
| - .isEqualTo("DELETE FROM no_id_chain0 WHERE no_id_chain0.no_id_chain4 = :rootId"); |
| 649 | + .isEqualTo("DELETE FROM no_id_chain0 WHERE no_id_chain0.no_id_chain4 = :rootId"); |
648 | 650 | }
|
649 | 651 |
|
650 | 652 | @Test // DATAJDBC-359
|
651 | 653 | void deletingLongChainNoIdWithBackreferenceNotReferencingTheRoot() {
|
652 | 654 |
|
653 | 655 | assertThat(createSqlGenerator(IdIdNoIdChain.class)
|
654 | 656 | .createDeleteByPath(getPath("idNoIdChain.chain4.chain3.chain2.chain1.chain0", IdIdNoIdChain.class))) //
|
655 |
| - .isEqualTo( // |
656 |
| - "DELETE FROM no_id_chain0 " // |
657 |
| - + "WHERE no_id_chain0.no_id_chain4 IN (" // |
658 |
| - + "SELECT no_id_chain4.x_four " // |
659 |
| - + "FROM no_id_chain4 " // |
660 |
| - + "WHERE no_id_chain4.id_no_id_chain IN (" // |
661 |
| - + "SELECT id_no_id_chain.x_id " // |
662 |
| - + "FROM id_no_id_chain " // |
663 |
| - + "WHERE id_no_id_chain.id_id_no_id_chain = :rootId" // |
664 |
| - + "))"); |
| 657 | + .isEqualTo( // |
| 658 | + "DELETE FROM no_id_chain0 " // |
| 659 | + + "WHERE no_id_chain0.no_id_chain4 IN (" // |
| 660 | + + "SELECT no_id_chain4.x_four " // |
| 661 | + + "FROM no_id_chain4 " // |
| 662 | + + "WHERE no_id_chain4.id_no_id_chain IN (" // |
| 663 | + + "SELECT id_no_id_chain.x_id " // |
| 664 | + + "FROM id_no_id_chain " // |
| 665 | + + "WHERE id_no_id_chain.id_id_no_id_chain = :rootId" // |
| 666 | + + "))"); |
665 | 667 | }
|
666 | 668 |
|
667 | 669 | @Test // DATAJDBC-340
|
@@ -926,6 +928,16 @@ void keyColumnShouldIgnoreRenamedParent() {
|
926 | 928 | "WHERE referenced_entity.parentId");
|
927 | 929 | }
|
928 | 930 |
|
| 931 | + @Test // GH-1865 |
| 932 | + void mappingMapKeyToChildShouldNotResultInDuplicateColumn() { |
| 933 | + |
| 934 | + SqlGenerator sqlGenerator = createSqlGenerator(Child.class); |
| 935 | + String sql = sqlGenerator.getFindAllByProperty(Identifier.of(unquoted("parent"), 23, Parent.class), |
| 936 | + context.getAggregatePath(getPath("children", Parent.class)).getTableInfo().qualifierColumnInfo(), false); |
| 937 | + |
| 938 | + assertThat(sql).containsOnlyOnce("child.NICK_NAME AS NICK_NAME"); |
| 939 | + } |
| 940 | + |
929 | 941 | @Nullable
|
930 | 942 | private SqlIdentifier getAlias(Object maybeAliased) {
|
931 | 943 |
|
@@ -1117,4 +1129,10 @@ static class IdIdNoIdChain {
|
1117 | 1129 | @Id Long id;
|
1118 | 1130 | IdNoIdChain idNoIdChain;
|
1119 | 1131 | }
|
| 1132 | + |
| 1133 | + record Parent(@Id Long id, String name, @MappedCollection(keyColumn = "NICK_NAME") Map<String, Child> children) { |
| 1134 | + } |
| 1135 | + |
| 1136 | + record Child(@Column("NICK_NAME") String nickName, String name) { |
| 1137 | + } |
1120 | 1138 | }
|
0 commit comments