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