diff --git a/spring-data-jdbc/src/main/java/org/springframework/data/jdbc/repository/query/StatementFactory.java b/spring-data-jdbc/src/main/java/org/springframework/data/jdbc/repository/query/StatementFactory.java index 1873a4f68..ee340d5cc 100644 --- a/spring-data-jdbc/src/main/java/org/springframework/data/jdbc/repository/query/StatementFactory.java +++ b/spring-data-jdbc/src/main/java/org/springframework/data/jdbc/repository/query/StatementFactory.java @@ -118,7 +118,7 @@ public class StatementFactory { private SelectionBuilder(RelationalPersistentEntity entity, Mode mode) { this.entity = entity; - this.table = Table.create(entity.getTableName()); + this.table = Table.create(entity.getQualifiedTableName()); this.mode = mode; } diff --git a/spring-data-jdbc/src/test/java/org/springframework/data/jdbc/repository/query/StatementFactoryUnitTests.java b/spring-data-jdbc/src/test/java/org/springframework/data/jdbc/repository/query/StatementFactoryUnitTests.java new file mode 100644 index 000000000..74d17d686 --- /dev/null +++ b/spring-data-jdbc/src/test/java/org/springframework/data/jdbc/repository/query/StatementFactoryUnitTests.java @@ -0,0 +1,70 @@ +/* + * Copyright 2025 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.data.jdbc.repository.query; + +import static org.assertj.core.api.Assertions.*; + +import java.util.List; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import org.springframework.data.annotation.Id; +import org.springframework.data.jdbc.core.convert.JdbcCustomConversions; +import org.springframework.data.jdbc.core.convert.JdbcTypeFactory; +import org.springframework.data.jdbc.core.convert.MappingJdbcConverter; +import org.springframework.data.jdbc.core.dialect.JdbcH2Dialect; +import org.springframework.data.jdbc.core.mapping.JdbcMappingContext; +import org.springframework.data.relational.core.mapping.Table; +import org.springframework.jdbc.core.namedparam.MapSqlParameterSource; + +/** + * Unit tests for {@link StatementFactory}. + * + * @author Mark Paluch + */ +class StatementFactoryUnitTests { + + JdbcMappingContext mappingContext = new JdbcMappingContext(); + MappingJdbcConverter converter; + + @BeforeEach + void setUp() { + + JdbcCustomConversions conversions = JdbcCustomConversions.of(JdbcH2Dialect.INSTANCE, List.of()); + mappingContext.setSimpleTypeHolder(conversions.getSimpleTypeHolder()); + mappingContext.afterPropertiesSet(); + converter = new MappingJdbcConverter(mappingContext, (identifier, path) -> null, conversions, + JdbcTypeFactory.unsupported()); + } + + @Test // GH-2162 + void statementFactoryConsidersQualifiedTableName() { + + MapSqlParameterSource parameterSource = new MapSqlParameterSource(); + + StatementFactory statementFactory = new StatementFactory(converter, JdbcH2Dialect.INSTANCE); + StatementFactory.SelectionBuilder selection = statementFactory.select(Media.class); + + String sql = selection.build(parameterSource); + assertThat(sql).contains("SELECT \"archive\".\"media\".").contains("ROM \"archive\".\"media\""); + } + + @Table(schema = "archive", name = "media") + public record Media(@Id Long id, String objectType, Long objectId) { + } + +}