Skip to content

Commit 614ce6c

Browse files
committed
Polishing.
Use ReturnedType.isDtoProjection/isInterfaceProjection methods. See #4088
1 parent d3aeb57 commit 614ce6c

File tree

8 files changed

+21
-17
lines changed

8 files changed

+21
-17
lines changed

spring-data-jpa/src/main/java/org/springframework/data/jpa/repository/aot/QueriesFactory.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -154,8 +154,7 @@ private AotQueries buildStringQuery(ReturnedType returnedType, QueryEnhancerSele
154154
StringAotQuery aotStringQuery = StringAotQuery.of(entityQuery);
155155
String countQuery = query.getString("countQuery");
156156

157-
if (returnedType.isProjecting() && returnedType.hasInputProperties()
158-
&& !returnedType.getReturnedType().isInterface()) {
157+
if (returnedType.isDtoProjection() && returnedType.hasInputProperties()) {
159158

160159
QueryProvider rewritten = entityQuery.rewrite(new QueryEnhancer.QueryRewriteInformation() {
161160
@Override
@@ -328,7 +327,7 @@ private AotQuery createCountQuery(PartTree partTree, ReturnedType returnedType,
328327

329328
if (returnedType.isProjecting()) {
330329

331-
if (returnedType.getReturnedType().isInterface()) {
330+
if (returnedType.isInterfaceProjection()) {
332331

333332
if (query.hasConstructorExpressionOrDefaultProjection()) {
334333
return result;

spring-data-jpa/src/main/java/org/springframework/data/jpa/repository/query/AbstractJpaQuery.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -303,7 +303,7 @@ protected Query createCountQuery(JpaParametersParameterAccessor values) {
303303
return null;
304304
}
305305

306-
return returnedType.isProjecting() && returnedType.getReturnedType().isInterface()
306+
return returnedType.isInterfaceProjection()
307307
&& !getMetamodel().isJpaManaged(returnedType.getReturnedType()) //
308308
? Tuple.class //
309309
: null;
@@ -374,7 +374,7 @@ public TupleConverter(ReturnedType type, boolean nativeQuery) {
374374

375375
this.type = type;
376376
this.tupleWrapper = nativeQuery ? TupleBackedMap::underscoreAware : UnaryOperator.identity();
377-
this.dtoProjection = type.isProjecting() && !type.getReturnedType().isInterface()
377+
this.dtoProjection = type.isDtoProjection()
378378
&& type.needsCustomConstruction();
379379

380380
if (this.dtoProjection) {

spring-data-jpa/src/main/java/org/springframework/data/jpa/repository/query/DtoProjectionTransformerDelegate.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ class DtoProjectionTransformerDelegate {
4646

4747
public DtoProjectionTransformerDelegate(ReturnedType returnedType) {
4848
this.returnedType = returnedType;
49-
this.applyRewriting = Lazy.of(() -> returnedType.isProjecting() && !returnedType.getReturnedType().isInterface()
49+
this.applyRewriting = Lazy.of(() -> returnedType.isDtoProjection()
5050
&& returnedType.needsCustomConstruction());
5151
}
5252

spring-data-jpa/src/main/java/org/springframework/data/jpa/repository/query/JpaQueryCreator.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ JpqlQueryBuilder.Entity getEntity() {
165165
}
166166

167167
public boolean useTupleQuery() {
168-
return returnedType.needsCustomConstruction() && returnedType.getReturnedType().isInterface();
168+
return returnedType.needsCustomConstruction() && returnedType.isInterfaceProjection();
169169
}
170170

171171
/**

spring-data-jpa/src/main/java/org/springframework/data/jpa/repository/query/NativeJpaQuery.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ protected Query createJpaQuery(QueryProvider declaredQuery, Sort sort, @Nullable
114114

115115
if (returnedType.isProjecting()) {
116116

117-
if (returnedType.getReturnedType().isInterface()) {
117+
if (returnedType.isInterfaceProjection()) {
118118
return Tuple.class;
119119
}
120120

spring-data-jpa/src/main/java/org/springframework/data/jpa/repository/support/FetchableFluentQueryByPredicate.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525
import java.util.function.Function;
2626
import java.util.stream.Stream;
2727

28+
import org.jspecify.annotations.Nullable;
29+
2830
import org.springframework.dao.IncorrectResultSizeDataAccessException;
2931
import org.springframework.data.domain.KeysetScrollPosition;
3032
import org.springframework.data.domain.Page;
@@ -51,7 +53,6 @@
5153
import com.querydsl.core.types.dsl.PathBuilder;
5254
import com.querydsl.jpa.JPQLSerializer;
5355
import com.querydsl.jpa.impl.AbstractJPAQuery;
54-
import org.jspecify.annotations.Nullable;
5556

5657
/**
5758
* Immutable implementation of {@link FetchableFluentQuery} based on a Querydsl {@link Predicate}. All methods that
@@ -231,7 +232,7 @@ private void applyQuerySettings(ReturnedType returnedType, int limit, AbstractJP
231232
if (returnedType.needsCustomConstruction()) {
232233

233234
Collection<String> requiredSelection;
234-
if (scrollPosition instanceof KeysetScrollPosition && returnedType.getReturnedType().isInterface()) {
235+
if (scrollPosition instanceof KeysetScrollPosition && returnedType.isInterfaceProjection()) {
235236
requiredSelection = KeysetScrollDelegate.getProjectionInputProperties(entityInformation, inputProperties, sort);
236237
} else {
237238
requiredSelection = inputProperties;
@@ -240,7 +241,7 @@ private void applyQuerySettings(ReturnedType returnedType, int limit, AbstractJP
240241
PathBuilder<?> builder = new PathBuilder<>(entityPath.getType(), entityPath.getMetadata());
241242
Expression<?>[] projection = requiredSelection.stream().map(builder::get).toArray(Expression[]::new);
242243

243-
if (returnedType.getReturnedType().isInterface()) {
244+
if (returnedType.isInterfaceProjection()) {
244245
query.select(new JakartaTuple(projection));
245246
} else {
246247
query.select(new DtoProjection(returnedType.getReturnedType(), projection));

spring-data-jpa/src/main/java/org/springframework/data/jpa/repository/support/SimpleJpaRepository.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -807,7 +807,7 @@ private <S extends T> TypedQuery<S> getQuery(ReturnedType returnedType, @Nullabl
807807
CriteriaBuilder builder = entityManager.getCriteriaBuilder();
808808
CriteriaQuery<S> query;
809809

810-
boolean interfaceProjection = returnedType.getReturnedType().isInterface();
810+
boolean interfaceProjection = returnedType.isInterfaceProjection();
811811

812812
if (returnedType.needsCustomConstruction() && (inputProperties.isEmpty() || !interfaceProjection)) {
813813
inputProperties = returnedType.getInputProperties();

spring-data-jpa/src/test/java/org/springframework/data/jpa/repository/query/TupleConverterUnitTests.java

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,6 @@ void setUp() throws Exception {
7373
}
7474

7575
@Test // DATAJPA-984
76-
@SuppressWarnings("unchecked")
7776
void returnsSingleTupleElementIfItMatchesExpectedType() {
7877

7978
doReturn(Collections.singletonList(element)).when(tuple).getElements();
@@ -85,7 +84,6 @@ void returnsSingleTupleElementIfItMatchesExpectedType() {
8584
}
8685

8786
@Test // DATAJPA-1024
88-
@SuppressWarnings("unchecked")
8987
void returnsNullForSingleElementTupleWithNullValue() {
9088

9189
doReturn(Collections.singletonList(element)).when(tuple).getElements();
@@ -130,13 +128,15 @@ void dealsWithNullsInArguments() {
130128
assertThat(result).isInstanceOf(WithPC.class);
131129
}
132130

133-
@Test // GH-3076
131+
@Test // GH-3076, GH-4088
134132
void fallsBackToCompatibleConstructor() {
135133

136134
ReturnedType returnedType = spy(
137135
ReturnedType.of(MultipleConstructors.class, DomainType.class, new SpelAwareProxyProjectionFactory()));
138136
when(returnedType.isProjecting()).thenReturn(true);
137+
when(returnedType.isDtoProjection()).thenReturn(true);
139138
when(returnedType.getInputProperties()).thenReturn(Arrays.asList("one", "two", "three"));
139+
when(returnedType.hasInputProperties()).thenReturn(true);
140140

141141
doReturn(List.of(element, element, element)).when(tuple).getElements();
142142
when(tuple.get(eq(0))).thenReturn("one");
@@ -163,13 +163,15 @@ void fallsBackToCompatibleConstructor() {
163163
assertThat(result.three).isEqualTo(97);
164164
}
165165

166-
@Test // GH-3076
166+
@Test // GH-3076, GH-4088
167167
void acceptsConstructorWithCastableType() {
168168

169169
ReturnedType returnedType = spy(
170170
ReturnedType.of(MultipleConstructors.class, DomainType.class, new SpelAwareProxyProjectionFactory()));
171171
when(returnedType.isProjecting()).thenReturn(true);
172+
when(returnedType.isDtoProjection()).thenReturn(true);
172173
when(returnedType.getInputProperties()).thenReturn(Arrays.asList("one", "two", "three", "four"));
174+
when(returnedType.hasInputProperties()).thenReturn(true);
173175

174176
doReturn(List.of(element, element, element, element)).when(tuple).getElements();
175177
when(tuple.get(eq(0))).thenReturn("one");
@@ -185,13 +187,15 @@ void acceptsConstructorWithCastableType() {
185187
assertThat(result.four).isEqualTo(2, offset(0.1d));
186188
}
187189

188-
@Test // GH-3076
190+
@Test // GH-3076, GH-4088
189191
void failsForNonResolvableConstructor() {
190192

191193
ReturnedType returnedType = spy(
192194
ReturnedType.of(MultipleConstructors.class, DomainType.class, new SpelAwareProxyProjectionFactory()));
193195
when(returnedType.isProjecting()).thenReturn(true);
196+
when(returnedType.isDtoProjection()).thenReturn(true);
194197
when(returnedType.getInputProperties()).thenReturn(Arrays.asList("one", "two"));
198+
when(returnedType.hasInputProperties()).thenReturn(true);
195199

196200
doReturn(List.of(element, element)).when(tuple).getElements();
197201
when(tuple.get(eq(0))).thenReturn(1);

0 commit comments

Comments
 (0)