Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,11 @@
package org.springframework.data.jdbc.repository.query;

import java.util.ArrayList;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;

import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
Expand Down Expand Up @@ -60,6 +62,7 @@
* @author Jens Schauder
* @author Myeonghyeon Lee
* @author Diego Krupitza
* @author Tomasz Bielecki
* @since 2.0
*/
class JdbcQueryCreator extends RelationalQueryCreator<ParametrizedQuery> {
Expand Down Expand Up @@ -235,7 +238,7 @@ SelectBuilder.SelectLimitOffset createSelectClause(RelationalPersistentEntity<?>

private SelectBuilder.SelectJoin selectBuilder(Table table) {

List<Expression> columnExpressions = new ArrayList<>();
Set<Expression> columnExpressions = new LinkedHashSet<>();
RelationalPersistentEntity<?> entity = entityMetadata.getTableEntity();
SqlContext sqlContext = new SqlContext(entity);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,14 @@
import org.springframework.data.projection.SpelAwareProxyProjectionFactory;
import org.springframework.data.relational.core.dialect.Escaper;
import org.springframework.data.relational.core.dialect.H2Dialect;
import org.springframework.data.relational.core.mapping.Column;
import org.springframework.data.relational.core.mapping.Embedded;
import org.springframework.data.relational.core.mapping.MappedCollection;
import org.springframework.data.relational.core.mapping.Table;
import org.springframework.data.relational.core.sql.LockMode;
import org.springframework.data.relational.repository.Lock;
import org.springframework.data.relational.repository.query.RelationalParametersParameterAccessor;
import org.springframework.data.repository.ListCrudRepository;
import org.springframework.data.repository.NoRepositoryBean;
import org.springframework.data.repository.Repository;
import org.springframework.data.repository.core.support.DefaultRepositoryMetadata;
Expand All @@ -60,6 +62,7 @@
* @author Jens Schauder
* @author Myeonghyeon Lee
* @author Diego Krupitza
* @author Tomasz Bielecki
*/
@ExtendWith(MockitoExtension.class)
public class PartTreeJdbcQueryUnitTests {
Expand Down Expand Up @@ -668,6 +671,17 @@ public void createsQueryForCountProjection() throws Exception {
.isEqualTo("SELECT COUNT(*) FROM " + TABLE + " WHERE " + TABLE + ".\"FIRST_NAME\" = :first_name");
}

@Test
public void mappingMapKeyToChildShouldNotResultInDuplicateColumn() throws Exception {
Method method = ParentRepository.class.getMethod("findByName", String.class);
var queryMethod = new JdbcQueryMethod(method, new DefaultRepositoryMetadata(ParentRepository.class),
new SpelAwareProxyProjectionFactory(), new PropertiesBasedNamedQueries(new Properties()), mappingContext);
PartTreeJdbcQuery jdbcQuery = createQuery(queryMethod);
ParametrizedQuery query = jdbcQuery.createQuery(getAccessor(queryMethod, new Object[] { "John" }), returnedType);

assertThat(query.getQuery()).containsOnlyOnce("\"children\".\"NICK_NAME\" AS \"children_NICK_NAME\"");
}

private PartTreeJdbcQuery createQuery(JdbcQueryMethod queryMethod) {
return new PartTreeJdbcQuery(mappingContext, queryMethod, H2Dialect.INSTANCE, converter,
mock(NamedParameterJdbcOperations.class), mock(RowMapper.class));
Expand Down Expand Up @@ -775,6 +789,19 @@ interface UserRepository extends Repository<User, Long> {
long countByFirstName(String name);
}

@NoRepositoryBean
interface ParentRepository extends ListCrudRepository<Parent, Long> {
Parent findByName(String name);
}

@Table("parent")
record Parent(@Id String name, @MappedCollection(idColumn = "NICK_NAME") Child children) {
}

@Table("children")
record Child(@Column("NICK_NAME") String nickName, String name) {
}

@Table("users")
static class User {

Expand Down