Skip to content

Commit d2cf55b

Browse files
committed
Consider fully-qualified table name in StatementFactory.
Closes #2162
1 parent 1db5190 commit d2cf55b

File tree

2 files changed

+71
-1
lines changed

2 files changed

+71
-1
lines changed

spring-data-jdbc/src/main/java/org/springframework/data/jdbc/repository/query/StatementFactory.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ public class SelectionBuilder {
118118

119119
private SelectionBuilder(RelationalPersistentEntity<?> entity, Mode mode) {
120120
this.entity = entity;
121-
this.table = Table.create(entity.getTableName());
121+
this.table = Table.create(entity.getQualifiedTableName());
122122
this.mode = mode;
123123
}
124124

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
/*
2+
* Copyright 2025 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.data.jdbc.repository.query;
17+
18+
import static org.assertj.core.api.Assertions.*;
19+
20+
import java.util.List;
21+
22+
import org.junit.jupiter.api.BeforeEach;
23+
import org.junit.jupiter.api.Test;
24+
25+
import org.springframework.data.annotation.Id;
26+
import org.springframework.data.jdbc.core.convert.JdbcCustomConversions;
27+
import org.springframework.data.jdbc.core.convert.JdbcTypeFactory;
28+
import org.springframework.data.jdbc.core.convert.MappingJdbcConverter;
29+
import org.springframework.data.jdbc.core.dialect.JdbcH2Dialect;
30+
import org.springframework.data.jdbc.core.mapping.JdbcMappingContext;
31+
import org.springframework.data.relational.core.mapping.Table;
32+
import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
33+
34+
/**
35+
* Unit tests for {@link StatementFactory}.
36+
*
37+
* @author Mark Paluch
38+
*/
39+
class StatementFactoryUnitTests {
40+
41+
JdbcMappingContext mappingContext = new JdbcMappingContext();
42+
MappingJdbcConverter converter;
43+
44+
@BeforeEach
45+
void setUp() {
46+
47+
JdbcCustomConversions conversions = JdbcCustomConversions.of(JdbcH2Dialect.INSTANCE, List.of());
48+
mappingContext.setSimpleTypeHolder(conversions.getSimpleTypeHolder());
49+
mappingContext.afterPropertiesSet();
50+
converter = new MappingJdbcConverter(mappingContext, (identifier, path) -> null, conversions,
51+
JdbcTypeFactory.unsupported());
52+
}
53+
54+
@Test // GH-2162
55+
void statementFactoryConsidersQualifiedTableName() {
56+
57+
MapSqlParameterSource parameterSource = new MapSqlParameterSource();
58+
59+
StatementFactory statementFactory = new StatementFactory(converter, JdbcH2Dialect.INSTANCE);
60+
StatementFactory.SelectionBuilder selection = statementFactory.select(Media.class);
61+
62+
String sql = selection.build(parameterSource);
63+
assertThat(sql).contains("SELECT \"archive\".\"media\".").contains("ROM \"archive\".\"media\"");
64+
}
65+
66+
@Table(schema = "archive", name = "media")
67+
public record Media(@Id Long id, String objectType, Long objectId) {
68+
}
69+
70+
}

0 commit comments

Comments
 (0)