Skip to content

Commit 2e868c6

Browse files
committed
Adopt to changed Parameters creation.
1 parent 256eb59 commit 2e868c6

File tree

7 files changed

+112
-41
lines changed

7 files changed

+112
-41
lines changed

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

+48-1
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,15 @@
2020
import java.lang.reflect.Method;
2121
import java.util.Date;
2222
import java.util.List;
23+
import java.util.function.Function;
2324

2425
import org.springframework.core.MethodParameter;
2526
import org.springframework.data.jpa.repository.Temporal;
2627
import org.springframework.data.jpa.repository.query.JpaParameters.JpaParameter;
28+
import org.springframework.data.repository.core.RepositoryMetadata;
2729
import org.springframework.data.repository.query.Parameter;
2830
import org.springframework.data.repository.query.Parameters;
31+
import org.springframework.data.util.TypeInformation;
2932
import org.springframework.lang.Nullable;
3033

3134
/**
@@ -41,16 +44,43 @@ public class JpaParameters extends Parameters<JpaParameters, JpaParameter> {
4144
* Creates a new {@link JpaParameters} instance from the given {@link Method}.
4245
*
4346
* @param method must not be {@literal null}.
47+
* @deprecated since 3.2.1, use {@link #JpaParameters(RepositoryMetadata, Method)} instead.
4448
*/
49+
@Deprecated(since = "3.2.1", forRemoval = true)
4550
public JpaParameters(Method method) {
46-
super(method);
51+
super(null, method, null);
52+
}
53+
54+
/**
55+
* Creates a new {@link JpaParameters} instance from the given {@link Method}.
56+
*
57+
* @param method must not be {@literal null}.
58+
* @param metadata not be {@literal null}.
59+
* @since 3.2.1
60+
*/
61+
public JpaParameters(RepositoryMetadata metadata, Method method) {
62+
super(metadata, method, methodParameter -> new JpaParameter(methodParameter, metadata.getDomainTypeInformation()));
63+
}
64+
65+
/**
66+
* Creates a new {@link JpaParameters} instance from the given {@link Method}.
67+
*
68+
* @param metadata must not be {@literal null}.
69+
* @param method must not be {@literal null}.
70+
* @param parameterFactory must not be {@literal null}.
71+
* @since 3.2.1
72+
*/
73+
protected JpaParameters(RepositoryMetadata metadata, Method method,
74+
Function<MethodParameter, JpaParameter> parameterFactory) {
75+
super(metadata, method, parameterFactory);
4776
}
4877

4978
private JpaParameters(List<JpaParameter> parameters) {
5079
super(parameters);
5180
}
5281

5382
@Override
83+
@Deprecated(forRemoval = true)
5484
protected JpaParameter createParameter(MethodParameter parameter) {
5585
return new JpaParameter(parameter);
5686
}
@@ -82,14 +112,31 @@ public static class JpaParameter extends Parameter {
82112
* Creates a new {@link JpaParameter}.
83113
*
84114
* @param parameter must not be {@literal null}.
115+
* @deprecated since 3.2.1
85116
*/
117+
@Deprecated(since = "3.2.1", forRemoval = true)
86118
protected JpaParameter(MethodParameter parameter) {
87119

88120
super(parameter);
89121

90122
this.annotation = parameter.getParameterAnnotation(Temporal.class);
91123
this.temporalType = null;
124+
if (!isDateParameter() && hasTemporalParamAnnotation()) {
125+
throw new IllegalArgumentException(
126+
Temporal.class.getSimpleName() + " annotation is only allowed on Date parameter");
127+
}
128+
}
92129

130+
/**
131+
* Creates a new {@link JpaParameter}.
132+
*
133+
* @param parameter must not be {@literal null}.
134+
*/
135+
protected JpaParameter(MethodParameter parameter, TypeInformation<?> domainType) {
136+
137+
super(parameter, domainType);
138+
this.annotation = parameter.getParameterAnnotation(Temporal.class);
139+
this.temporalType = null;
93140
if (!isDateParameter() && hasTemporalParamAnnotation()) {
94141
throw new IllegalArgumentException(
95142
Temporal.class.getSimpleName() + " annotation is only allowed on Date parameter");

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -447,8 +447,8 @@ private <T> T getMergedOrDefaultAnnotationValue(String attribute, Class annotati
447447
}
448448

449449
@Override
450-
protected JpaParameters createParameters(Method method) {
451-
return new JpaParameters(method);
450+
protected Parameters<?, ?> createParameters(RepositoryMetadata metadata, Method method) {
451+
return new JpaParameters(metadata, method);
452452
}
453453

454454
@Override

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

+8-4
Original file line numberDiff line numberDiff line change
@@ -18,20 +18,24 @@
1818
import static jakarta.persistence.TemporalType.*;
1919
import static org.assertj.core.api.Assertions.*;
2020

21+
import jakarta.persistence.TemporalType;
22+
2123
import java.lang.reflect.Method;
2224
import java.util.Date;
2325

24-
import jakarta.persistence.TemporalType;
25-
2626
import org.junit.jupiter.api.Test;
27+
2728
import org.springframework.data.jpa.repository.Temporal;
2829
import org.springframework.data.jpa.repository.query.JpaParameters.JpaParameter;
30+
import org.springframework.data.repository.Repository;
31+
import org.springframework.data.repository.core.support.DefaultRepositoryMetadata;
2932

3033
/**
3134
* Unit tests for {@link JpaParameters}.
3235
*
3336
* @author Oliver Gierke
3437
* @author Jens Schauder
38+
* @author Mark Paluch
3539
*/
3640
class JpaParametersUnitTests {
3741

@@ -40,7 +44,7 @@ void findsTemporalParameterConfiguration() throws Exception {
4044

4145
Method method = SampleRepository.class.getMethod("foo", Date.class, String.class);
4246

43-
JpaParameters parameters = new JpaParameters(method);
47+
JpaParameters parameters = new JpaParameters(new DefaultRepositoryMetadata(SampleRepository.class), method);
4448

4549
JpaParameter parameter = parameters.getBindableParameter(0);
4650
assertThat(parameter.isSpecialParameter()).isFalse();
@@ -51,7 +55,7 @@ void findsTemporalParameterConfiguration() throws Exception {
5155
assertThat(parameter.isTemporalParameter()).isFalse();
5256
}
5357

54-
interface SampleRepository {
58+
interface SampleRepository extends Repository<String, String> {
5559

5660
void foo(@Temporal(TIMESTAMP) Date date, String firstname);
5761
}

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

+25-10
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@
6868
* @author Mark Paluch
6969
* @author Erik Pellizzon
7070
*/
71+
@SuppressWarnings({"rawtypes", "unchecked"})
7172
@ExtendWith(MockitoExtension.class)
7273
@MockitoSettings(strictness = Strictness.LENIENT)
7374
class JpaQueryMethodUnitTests {
@@ -156,6 +157,8 @@ void returnsQueryIfAvailable() throws Exception {
156157
void rejectsInvalidReturntypeOnPagebleFinder() {
157158

158159
when(metadata.getReturnedDomainClass(any())).thenReturn((Class) User.class);
160+
when(metadata.getDomainTypeInformation()).thenReturn((TypeInformation) TypeInformation.of(User.class));
161+
when(metadata.getRepositoryInterface()).thenReturn((Class) InvalidRepository.class);
159162

160163
assertThatIllegalStateException()
161164
.isThrownBy(() -> new JpaQueryMethod(invalidReturnType, metadata, factory, extractor));
@@ -165,6 +168,8 @@ void rejectsInvalidReturntypeOnPagebleFinder() {
165168
void rejectsPageableAndSortInFinderMethod() {
166169

167170
when(metadata.getReturnedDomainClass(any())).thenReturn((Class) User.class);
171+
when(metadata.getDomainTypeInformation()).thenReturn((TypeInformation) TypeInformation.of(User.class));
172+
when(metadata.getRepositoryInterface()).thenReturn((Class) InvalidRepository.class);
168173

169174
assertThatIllegalStateException()
170175
.isThrownBy(() -> new JpaQueryMethod(pageableAndSort, metadata, factory, extractor));
@@ -318,8 +323,10 @@ void detectsLockAndQueryHintsOnIfUsedAsMetaAnnotation() throws Exception {
318323
@Test // DATAJPA-466
319324
void shouldStoreJpa21FetchGraphInformationAsHint() {
320325

321-
doReturn(User.class).when(metadata).getDomainType();
322-
doReturn(User.class).when(metadata).getReturnedDomainClass(queryMethodWithCustomEntityFetchGraph);
326+
when(metadata.getDomainType()).thenReturn((Class) User.class);
327+
when(metadata.getReturnedDomainClass(queryMethodWithCustomEntityFetchGraph)).thenReturn((Class) User.class);
328+
when(metadata.getDomainTypeInformation()).thenReturn((TypeInformation) TypeInformation.of(User.class));
329+
when(metadata.getRepositoryInterface()).thenReturn((Class) InvalidRepository.class);
323330

324331
JpaQueryMethod method = new JpaQueryMethod(queryMethodWithCustomEntityFetchGraph, metadata, factory, extractor);
325332

@@ -331,8 +338,10 @@ void shouldStoreJpa21FetchGraphInformationAsHint() {
331338
@Test // DATAJPA-612
332339
void shouldFindEntityGraphAnnotationOnOverriddenSimpleJpaRepositoryMethod() throws Exception {
333340

334-
doReturn(User.class).when(metadata).getDomainType();
335-
doReturn(User.class).when(metadata).getReturnedDomainClass((Method) any());
341+
when(metadata.getDomainType()).thenReturn((Class) User.class);
342+
when(metadata.getReturnedDomainClass(any())).thenReturn((Class) User.class);
343+
when(metadata.getReturnedDomainClass(queryMethodWithCustomEntityFetchGraph)).thenReturn((Class) User.class);
344+
when(metadata.getRepositoryInterface()).thenReturn((Class) JpaRepositoryOverride.class);
336345

337346
JpaQueryMethod method = new JpaQueryMethod(JpaRepositoryOverride.class.getMethod("findAll"), metadata, factory,
338347
extractor);
@@ -345,8 +354,10 @@ void shouldFindEntityGraphAnnotationOnOverriddenSimpleJpaRepositoryMethod() thro
345354
@Test // DATAJPA-689
346355
void shouldFindEntityGraphAnnotationOnOverriddenSimpleJpaRepositoryMethodFindOne() throws Exception {
347356

348-
doReturn(User.class).when(metadata).getDomainType();
349-
doReturn(User.class).when(metadata).getReturnedDomainClass((Method) any());
357+
when(metadata.getDomainType()).thenReturn((Class) User.class);
358+
when(metadata.getReturnedDomainClass(any())).thenReturn((Class) User.class);
359+
when(metadata.getDomainTypeInformation()).thenReturn((TypeInformation) TypeInformation.of(User.class));
360+
when(metadata.getRepositoryInterface()).thenReturn((Class) InvalidRepository.class);
350361

351362
JpaQueryMethod method = new JpaQueryMethod(JpaRepositoryOverride.class.getMethod("findOne", Integer.class),
352363
metadata, factory, extractor);
@@ -362,8 +373,10 @@ void shouldFindEntityGraphAnnotationOnOverriddenSimpleJpaRepositoryMethodFindOne
362373
@Test
363374
void shouldFindEntityGraphAnnotationOnQueryMethodGetOneByWithDerivedName() throws Exception {
364375

365-
doReturn(User.class).when(metadata).getDomainType();
366-
doReturn(User.class).when(metadata).getReturnedDomainClass((Method) any());
376+
when(metadata.getDomainType()).thenReturn((Class) User.class);
377+
when(metadata.getDomainTypeInformation()).thenReturn((TypeInformation) TypeInformation.of(User.class));
378+
when(metadata.getReturnedDomainClass(any())).thenReturn((Class) User.class);
379+
when(metadata.getRepositoryInterface()).thenReturn((Class) JpaRepositoryOverride.class);
367380

368381
JpaQueryMethod method = new JpaQueryMethod(JpaRepositoryOverride.class.getMethod("getOneById", Integer.class),
369382
metadata, factory, extractor);
@@ -473,8 +486,10 @@ void usesAliasedValueForQueryNativeQuery() throws Exception {
473486
@Test // DATAJPA-871
474487
void usesAliasedValueForEntityGraph() throws Exception {
475488

476-
doReturn(User.class).when(metadata).getDomainType();
477-
doReturn(User.class).when(metadata).getReturnedDomainClass((Method) any());
489+
when(metadata.getDomainType()).thenReturn((Class) User.class);
490+
when(metadata.getDomainTypeInformation()).thenReturn((TypeInformation) TypeInformation.of(User.class));
491+
when(metadata.getReturnedDomainClass(any())).thenReturn((Class) User.class);
492+
when(metadata.getRepositoryInterface()).thenReturn((Class) JpaRepositoryOverride.class);
478493

479494
JpaQueryMethod method = new JpaQueryMethod(
480495
JpaRepositoryOverride.class.getMethod("getOneWithCustomEntityGraphAnnotation"), metadata, factory, extractor);

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

+5-8
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,9 @@
1515
*/
1616
package org.springframework.data.jpa.repository.query;
1717

18-
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
19-
import static org.mockito.ArgumentMatchers.any;
20-
import static org.mockito.ArgumentMatchers.eq;
21-
import static org.mockito.Mockito.mock;
22-
import static org.mockito.Mockito.never;
23-
import static org.mockito.Mockito.times;
24-
import static org.mockito.Mockito.verify;
25-
import static org.mockito.Mockito.when;
18+
import static org.assertj.core.api.Assertions.*;
19+
import static org.mockito.ArgumentMatchers.*;
20+
import static org.mockito.Mockito.*;
2621

2722
import jakarta.persistence.EntityManager;
2823
import jakarta.persistence.EntityManagerFactory;
@@ -38,6 +33,7 @@
3833
import org.mockito.junit.jupiter.MockitoExtension;
3934
import org.mockito.junit.jupiter.MockitoSettings;
4035
import org.mockito.quality.Strictness;
36+
4137
import org.springframework.data.domain.Page;
4238
import org.springframework.data.domain.Pageable;
4339
import org.springframework.data.jpa.provider.QueryExtractor;
@@ -75,6 +71,7 @@ void setUp() throws SecurityException, NoSuchMethodException {
7571

7672
method = SampleRepository.class.getMethod("foo", Pageable.class);
7773
when(metadata.getDomainType()).thenReturn((Class) String.class);
74+
when(metadata.getDomainTypeInformation()).thenReturn((TypeInformation) TypeInformation.of(String.class));
7875
when(metadata.getReturnedDomainClass(method)).thenReturn((Class) String.class);
7976
when(metadata.getReturnType(any(Method.class)))
8077
.thenAnswer(invocation -> TypeInformation.fromReturnTypeOf(invocation.getArgument(0)));

0 commit comments

Comments
 (0)