Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adopt JpaParameters to reflect the actual parameter type when using generics #3257

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

<groupId>org.springframework.data</groupId>
<artifactId>spring-data-jpa-parent</artifactId>
<version>3.3.0-SNAPSHOT</version>
<version>3.3.0-GH-3254-SNAPSHOT</version>
<packaging>pom</packaging>

<name>Spring Data JPA Parent</name>
Expand Down Expand Up @@ -37,7 +37,7 @@
<jsqlparser>4.5</jsqlparser>
<mysql-connector-java>8.0.33</mysql-connector-java>
<postgresql>42.6.0</postgresql>
<springdata.commons>3.3.0-SNAPSHOT</springdata.commons>
<springdata.commons>3.3.0-GH-2995-SNAPSHOT</springdata.commons>
<vavr>0.10.3</vavr>

<hibernate.groupId>org.hibernate</hibernate.groupId>
Expand Down
4 changes: 2 additions & 2 deletions spring-data-envers/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@

<groupId>org.springframework.data</groupId>
<artifactId>spring-data-envers</artifactId>
<version>3.3.0-SNAPSHOT</version>
<version>3.3.0-GH-3254-SNAPSHOT</version>

<parent>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-jpa-parent</artifactId>
<version>3.3.0-SNAPSHOT</version>
<version>3.3.0-GH-3254-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down
2 changes: 1 addition & 1 deletion spring-data-jpa-distribution/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
<parent>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-jpa-parent</artifactId>
<version>3.3.0-SNAPSHOT</version>
<version>3.3.0-GH-3254-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down
4 changes: 2 additions & 2 deletions spring-data-jpa/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>org.springframework.data</groupId>
<artifactId>spring-data-jpa</artifactId>
<version>3.3.0-SNAPSHOT</version>
<version>3.3.0-GH-3254-SNAPSHOT</version>

<name>Spring Data JPA</name>
<description>Spring Data module for JPA repositories.</description>
Expand All @@ -15,7 +15,7 @@
<parent>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-jpa-parent</artifactId>
<version>3.3.0-SNAPSHOT</version>
<version>3.3.0-GH-3254-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,15 @@
import java.lang.reflect.Method;
import java.util.Date;
import java.util.List;
import java.util.function.Function;

import org.springframework.core.MethodParameter;
import org.springframework.data.jpa.repository.Temporal;
import org.springframework.data.jpa.repository.query.JpaParameters.JpaParameter;
import org.springframework.data.repository.core.RepositoryMetadata;
import org.springframework.data.repository.query.Parameter;
import org.springframework.data.repository.query.Parameters;
import org.springframework.data.util.TypeInformation;
import org.springframework.lang.Nullable;

/**
Expand All @@ -41,16 +44,43 @@ public class JpaParameters extends Parameters<JpaParameters, JpaParameter> {
* Creates a new {@link JpaParameters} instance from the given {@link Method}.
*
* @param method must not be {@literal null}.
* @deprecated since 3.2.1, use {@link #JpaParameters(RepositoryMetadata, Method)} instead.
*/
@Deprecated(since = "3.2.1", forRemoval = true)
public JpaParameters(Method method) {
super(method);
super(null, method, null);
}

/**
* Creates a new {@link JpaParameters} instance from the given {@link Method}.
*
* @param method must not be {@literal null}.
* @param metadata not be {@literal null}.
* @since 3.2.1
*/
public JpaParameters(RepositoryMetadata metadata, Method method) {
super(metadata, method, methodParameter -> new JpaParameter(methodParameter, metadata.getDomainTypeInformation()));
}

/**
* Creates a new {@link JpaParameters} instance from the given {@link Method}.
*
* @param metadata must not be {@literal null}.
* @param method must not be {@literal null}.
* @param parameterFactory must not be {@literal null}.
* @since 3.2.1
*/
protected JpaParameters(RepositoryMetadata metadata, Method method,
Function<MethodParameter, JpaParameter> parameterFactory) {
super(metadata, method, parameterFactory);
}

private JpaParameters(List<JpaParameter> parameters) {
super(parameters);
}

@Override
@Deprecated(forRemoval = true)
protected JpaParameter createParameter(MethodParameter parameter) {
return new JpaParameter(parameter);
}
Expand Down Expand Up @@ -82,14 +112,31 @@ public static class JpaParameter extends Parameter {
* Creates a new {@link JpaParameter}.
*
* @param parameter must not be {@literal null}.
* @deprecated since 3.2.1
*/
@Deprecated(since = "3.2.1", forRemoval = true)
protected JpaParameter(MethodParameter parameter) {

super(parameter);

this.annotation = parameter.getParameterAnnotation(Temporal.class);
this.temporalType = null;
if (!isDateParameter() && hasTemporalParamAnnotation()) {
throw new IllegalArgumentException(
Temporal.class.getSimpleName() + " annotation is only allowed on Date parameter");
}
}

/**
* Creates a new {@link JpaParameter}.
*
* @param parameter must not be {@literal null}.
*/
protected JpaParameter(MethodParameter parameter, TypeInformation<?> domainType) {

super(parameter, domainType);
this.annotation = parameter.getParameterAnnotation(Temporal.class);
this.temporalType = null;
if (!isDateParameter() && hasTemporalParamAnnotation()) {
throw new IllegalArgumentException(
Temporal.class.getSimpleName() + " annotation is only allowed on Date parameter");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -447,8 +447,8 @@ private <T> T getMergedOrDefaultAnnotationValue(String attribute, Class annotati
}

@Override
protected JpaParameters createParameters(Method method) {
return new JpaParameters(method);
protected JpaParameters createParameters(RepositoryMetadata metadata, Method method) {
return new JpaParameters(metadata, method);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,24 @@
import static jakarta.persistence.TemporalType.*;
import static org.assertj.core.api.Assertions.*;

import jakarta.persistence.TemporalType;

import java.lang.reflect.Method;
import java.util.Date;

import jakarta.persistence.TemporalType;

import org.junit.jupiter.api.Test;

import org.springframework.data.jpa.repository.Temporal;
import org.springframework.data.jpa.repository.query.JpaParameters.JpaParameter;
import org.springframework.data.repository.Repository;
import org.springframework.data.repository.core.support.DefaultRepositoryMetadata;

/**
* Unit tests for {@link JpaParameters}.
*
* @author Oliver Gierke
* @author Jens Schauder
* @author Mark Paluch
*/
class JpaParametersUnitTests {

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

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

JpaParameters parameters = new JpaParameters(method);
JpaParameters parameters = new JpaParameters(new DefaultRepositoryMetadata(SampleRepository.class), method);

JpaParameter parameter = parameters.getBindableParameter(0);
assertThat(parameter.isSpecialParameter()).isFalse();
Expand All @@ -51,7 +55,7 @@ void findsTemporalParameterConfiguration() throws Exception {
assertThat(parameter.isTemporalParameter()).isFalse();
}

interface SampleRepository {
interface SampleRepository extends Repository<String, String> {

void foo(@Temporal(TIMESTAMP) Date date, String firstname);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@
* @author Mark Paluch
* @author Erik Pellizzon
*/
@SuppressWarnings({"rawtypes", "unchecked"})
@ExtendWith(MockitoExtension.class)
@MockitoSettings(strictness = Strictness.LENIENT)
class JpaQueryMethodUnitTests {
Expand Down Expand Up @@ -156,6 +157,8 @@ void returnsQueryIfAvailable() throws Exception {
void rejectsInvalidReturntypeOnPagebleFinder() {

when(metadata.getReturnedDomainClass(any())).thenReturn((Class) User.class);
when(metadata.getDomainTypeInformation()).thenReturn((TypeInformation) TypeInformation.of(User.class));
when(metadata.getRepositoryInterface()).thenReturn((Class) InvalidRepository.class);

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

when(metadata.getReturnedDomainClass(any())).thenReturn((Class) User.class);
when(metadata.getDomainTypeInformation()).thenReturn((TypeInformation) TypeInformation.of(User.class));
when(metadata.getRepositoryInterface()).thenReturn((Class) InvalidRepository.class);

assertThatIllegalStateException()
.isThrownBy(() -> new JpaQueryMethod(pageableAndSort, metadata, factory, extractor));
Expand Down Expand Up @@ -318,8 +323,10 @@ void detectsLockAndQueryHintsOnIfUsedAsMetaAnnotation() throws Exception {
@Test // DATAJPA-466
void shouldStoreJpa21FetchGraphInformationAsHint() {

doReturn(User.class).when(metadata).getDomainType();
doReturn(User.class).when(metadata).getReturnedDomainClass(queryMethodWithCustomEntityFetchGraph);
when(metadata.getDomainType()).thenReturn((Class) User.class);
when(metadata.getReturnedDomainClass(queryMethodWithCustomEntityFetchGraph)).thenReturn((Class) User.class);
when(metadata.getDomainTypeInformation()).thenReturn((TypeInformation) TypeInformation.of(User.class));
when(metadata.getRepositoryInterface()).thenReturn((Class) InvalidRepository.class);

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

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

doReturn(User.class).when(metadata).getDomainType();
doReturn(User.class).when(metadata).getReturnedDomainClass((Method) any());
when(metadata.getDomainType()).thenReturn((Class) User.class);
when(metadata.getReturnedDomainClass(any())).thenReturn((Class) User.class);
when(metadata.getReturnedDomainClass(queryMethodWithCustomEntityFetchGraph)).thenReturn((Class) User.class);
when(metadata.getRepositoryInterface()).thenReturn((Class) JpaRepositoryOverride.class);

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

doReturn(User.class).when(metadata).getDomainType();
doReturn(User.class).when(metadata).getReturnedDomainClass((Method) any());
when(metadata.getDomainType()).thenReturn((Class) User.class);
when(metadata.getReturnedDomainClass(any())).thenReturn((Class) User.class);
when(metadata.getDomainTypeInformation()).thenReturn((TypeInformation) TypeInformation.of(User.class));
when(metadata.getRepositoryInterface()).thenReturn((Class) InvalidRepository.class);

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

doReturn(User.class).when(metadata).getDomainType();
doReturn(User.class).when(metadata).getReturnedDomainClass((Method) any());
when(metadata.getDomainType()).thenReturn((Class) User.class);
when(metadata.getDomainTypeInformation()).thenReturn((TypeInformation) TypeInformation.of(User.class));
when(metadata.getReturnedDomainClass(any())).thenReturn((Class) User.class);
when(metadata.getRepositoryInterface()).thenReturn((Class) JpaRepositoryOverride.class);

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

doReturn(User.class).when(metadata).getDomainType();
doReturn(User.class).when(metadata).getReturnedDomainClass((Method) any());
when(metadata.getDomainType()).thenReturn((Class) User.class);
when(metadata.getDomainTypeInformation()).thenReturn((TypeInformation) TypeInformation.of(User.class));
when(metadata.getReturnedDomainClass(any())).thenReturn((Class) User.class);
when(metadata.getRepositoryInterface()).thenReturn((Class) JpaRepositoryOverride.class);

JpaQueryMethod method = new JpaQueryMethod(
JpaRepositoryOverride.class.getMethod("getOneWithCustomEntityGraphAnnotation"), metadata, factory, extractor);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,9 @@
*/
package org.springframework.data.jpa.repository.query;

import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import static org.assertj.core.api.Assertions.*;
import static org.mockito.ArgumentMatchers.*;
import static org.mockito.Mockito.*;

import jakarta.persistence.EntityManager;
import jakarta.persistence.EntityManagerFactory;
Expand All @@ -38,6 +33,7 @@
import org.mockito.junit.jupiter.MockitoExtension;
import org.mockito.junit.jupiter.MockitoSettings;
import org.mockito.quality.Strictness;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.provider.QueryExtractor;
Expand Down Expand Up @@ -75,6 +71,7 @@ void setUp() throws SecurityException, NoSuchMethodException {

method = SampleRepository.class.getMethod("foo", Pageable.class);
when(metadata.getDomainType()).thenReturn((Class) String.class);
when(metadata.getDomainTypeInformation()).thenReturn((TypeInformation) TypeInformation.of(String.class));
when(metadata.getReturnedDomainClass(method)).thenReturn((Class) String.class);
when(metadata.getReturnType(any(Method.class)))
.thenAnswer(invocation -> TypeInformation.fromReturnTypeOf(invocation.getArgument(0)));
Expand Down
Loading