Skip to content

Commit b99b4aa

Browse files
committed
Avoid unnecessary joins.
Original pull request #980
1 parent f08b5ca commit b99b4aa

File tree

2 files changed

+9
-7
lines changed

2 files changed

+9
-7
lines changed

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

+6-6
Original file line numberDiff line numberDiff line change
@@ -241,19 +241,19 @@ private SelectBuilder.SelectJoin selectBuilder(Table table) {
241241

242242
PersistentPropertyPathExtension extPath = new PersistentPropertyPathExtension(context, path);
243243

244-
// add a join if necessary
245-
Join join = getJoin(sqlContext, extPath);
246-
if (join != null) {
247-
joinTables.add(join);
248-
}
249-
250244
if (returnedType.needsCustomConstruction()) {
251245
if (!returnedType.getInputProperties()
252246
.contains(extPath.getRequiredPersistentPropertyPath().getBaseProperty().getName())) {
253247
continue;
254248
}
255249
}
256250

251+
// add a join if necessary
252+
Join join = getJoin(sqlContext, extPath);
253+
if (join != null) {
254+
joinTables.add(join);
255+
}
256+
257257
Column column = getColumn(sqlContext, extPath);
258258
if (column != null) {
259259
columnExpressions.add(column);

spring-data-jdbc/src/test/java/org/springframework/data/jdbc/repository/query/PartTreeJdbcQueryUnitTests.java

+3-1
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ public void createsQueryToFindAllEntitiesByProjectionAttribute() throws Exceptio
126126
PartTreeJdbcQuery jdbcQuery = createQuery(queryMethod);
127127
ParametrizedQuery query = jdbcQuery.createQuery(getAccessor(queryMethod, new Object[] { "John" }), returnedType);
128128

129-
assertThat(query.getQuery()).isEqualTo("SELECT " + TABLE + ".\"FIRST_NAME\" AS \"FIRST_NAME\" " + JOIN_CLAUSE
129+
assertThat(query.getQuery()).isEqualTo("SELECT " + TABLE + ".\"FIRST_NAME\" AS \"FIRST_NAME\" FROM \"users\""
130130
+ " WHERE " + TABLE + ".\"FIRST_NAME\" = :first_name");
131131
}
132132

@@ -601,6 +601,8 @@ interface UserRepository extends Repository<User, Long> {
601601

602602
List<User> findAllByHated(Hobby hobby);
603603

604+
List<User> findAllByHatedName(String name);
605+
604606
List<User> findAllByHobbies(Object hobbies);
605607

606608
List<User> findAllByHobbyReference(Hobby hobby);

0 commit comments

Comments
 (0)