|
38 | 38 | import org.springframework.data.projection.SpelAwareProxyProjectionFactory; |
39 | 39 | import org.springframework.data.relational.core.dialect.Escaper; |
40 | 40 | import org.springframework.data.relational.core.dialect.H2Dialect; |
| 41 | +import org.springframework.data.relational.core.mapping.Column; |
41 | 42 | import org.springframework.data.relational.core.mapping.Embedded; |
42 | 43 | import org.springframework.data.relational.core.mapping.MappedCollection; |
43 | 44 | import org.springframework.data.relational.core.mapping.Table; |
44 | 45 | import org.springframework.data.relational.core.sql.LockMode; |
45 | 46 | import org.springframework.data.relational.repository.Lock; |
46 | 47 | import org.springframework.data.relational.repository.query.RelationalParametersParameterAccessor; |
| 48 | +import org.springframework.data.repository.ListCrudRepository; |
47 | 49 | import org.springframework.data.repository.NoRepositoryBean; |
48 | 50 | import org.springframework.data.repository.Repository; |
49 | 51 | import org.springframework.data.repository.core.support.DefaultRepositoryMetadata; |
|
60 | 62 | * @author Jens Schauder |
61 | 63 | * @author Myeonghyeon Lee |
62 | 64 | * @author Diego Krupitza |
| 65 | + * @author Tomasz Bielecki |
63 | 66 | */ |
64 | 67 | @ExtendWith(MockitoExtension.class) |
65 | 68 | public class PartTreeJdbcQueryUnitTests { |
@@ -668,6 +671,17 @@ public void createsQueryForCountProjection() throws Exception { |
668 | 671 | .isEqualTo("SELECT COUNT(*) FROM " + TABLE + " WHERE " + TABLE + ".\"FIRST_NAME\" = :first_name"); |
669 | 672 | } |
670 | 673 |
|
| 674 | + @Test |
| 675 | + public void mappingMapKeyToChildShouldNotResultInDuplicateColumn() throws Exception { |
| 676 | + Method method = ParentRepository.class.getMethod("findByName", String.class); |
| 677 | + var queryMethod = new JdbcQueryMethod(method, new DefaultRepositoryMetadata(ParentRepository.class), |
| 678 | + new SpelAwareProxyProjectionFactory(), new PropertiesBasedNamedQueries(new Properties()), mappingContext); |
| 679 | + PartTreeJdbcQuery jdbcQuery = createQuery(queryMethod); |
| 680 | + ParametrizedQuery query = jdbcQuery.createQuery(getAccessor(queryMethod, new Object[] { "John" }), returnedType); |
| 681 | + |
| 682 | + assertThat(query.getQuery()).containsOnlyOnce("\"children\".\"NICK_NAME\" AS \"children_NICK_NAME\""); |
| 683 | + } |
| 684 | + |
671 | 685 | private PartTreeJdbcQuery createQuery(JdbcQueryMethod queryMethod) { |
672 | 686 | return new PartTreeJdbcQuery(mappingContext, queryMethod, H2Dialect.INSTANCE, converter, |
673 | 687 | mock(NamedParameterJdbcOperations.class), mock(RowMapper.class)); |
@@ -775,6 +789,19 @@ interface UserRepository extends Repository<User, Long> { |
775 | 789 | long countByFirstName(String name); |
776 | 790 | } |
777 | 791 |
|
| 792 | + @NoRepositoryBean |
| 793 | + interface ParentRepository extends ListCrudRepository<Parent, Long> { |
| 794 | + Parent findByName(String name); |
| 795 | + } |
| 796 | + |
| 797 | + @Table("parent") |
| 798 | + record Parent(@Id String name, @MappedCollection(idColumn = "NICK_NAME") Child children) { |
| 799 | + } |
| 800 | + |
| 801 | + @Table("children") |
| 802 | + record Child(@Column("NICK_NAME") String nickName, String name) { |
| 803 | + } |
| 804 | + |
778 | 805 | @Table("users") |
779 | 806 | static class User { |
780 | 807 |
|
|
0 commit comments