Skip to content

Extend JpaParametersParameterAccessor to offer extractDate fuction #2859

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

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
2 changes: 1 addition & 1 deletion 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.1.0-SNAPSHOT</version>
<version>3.1.0-gh-2857-SNAPSHOT</version>
<packaging>pom</packaging>

<name>Spring Data JPA Parent</name>
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.1.0-SNAPSHOT</version>
<version>3.1.0-gh-2857-SNAPSHOT</version>

<parent>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-jpa-parent</artifactId>
<version>3.1.0-SNAPSHOT</version>
<version>3.1.0-gh-2857-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.1.0-SNAPSHOT</version>
<version>3.1.0-gh-2857-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.1.0-SNAPSHOT</version>
<version>3.1.0-gh-2857-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.1.0-SNAPSHOT</version>
<version>3.1.0-gh-2857-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@

import jakarta.persistence.EntityManager;

import java.util.Date;

import org.hibernate.engine.spi.SessionFactoryImplementor;
import org.hibernate.query.TypedParameterValue;
import org.hibernate.type.BasicTypeRegistry;
Expand All @@ -36,6 +38,7 @@
* @author Cedomir Igaly
* @author Robert Wilson
* @author Oliver Drotbohm
* @author Greg Turnquist
* @since 2.7
*/
class HibernateJpaParametersParameterAccessor extends JpaParametersParameterAccessor {
Expand All @@ -53,9 +56,9 @@ class HibernateJpaParametersParameterAccessor extends JpaParametersParameterAcce

super(parameters, values);

this.typeHelper = em.getEntityManagerFactory()
.unwrap(SessionFactoryImplementor.class)
.getTypeConfiguration()
this.typeHelper = em.getEntityManagerFactory() //
.unwrap(SessionFactoryImplementor.class) //
.getTypeConfiguration() //
.getBasicTypeRegistry();
}

Expand All @@ -78,4 +81,19 @@ public Object getValue(Parameter parameter) {

return new TypedParameterValue<>(type, null);
}

/**
* For Hibernate, check if the incoming value is wrapped inside a {@link TypedParameterValue} before extracting and
* casting the {@link Date}.
*
* @param extractedValue
* @since 3.1
*/
@Override
public Date extractDate(Object extractedValue) {

return (extractedValue instanceof TypedParameterValue<?> typedParameterValue)
? (Date) typedParameterValue.getValue()
: (Date) extractedValue;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
*/
package org.springframework.data.jpa.repository.query;

import java.util.Date;

import org.springframework.data.jpa.repository.query.JpaParameters.JpaParameter;
import org.springframework.data.repository.query.Parameter;
import org.springframework.data.repository.query.Parameters;
Expand All @@ -27,6 +29,7 @@
*
* @author Jens Schauder
* @author Mark Paluch
* @author Greg Turnquist
*/
public class JpaParametersParameterAccessor extends ParametersParameterAccessor {

Expand All @@ -49,4 +52,15 @@ public <T> T getValue(Parameter parameter) {
public Object[] getValues() {
return super.getValues();
}

/**
* For general JPA providers, simply pass through the extracted value, casting it as a {@link Date}.
*
* @param extractedValue
* @since 3.1
*/
public Date extractDate(Object extractedValue) {
return (Date) extractedValue;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
*/
package org.springframework.data.jpa.repository.query;

import static org.springframework.data.jpa.repository.query.QueryParameterSetter.ErrorHandling.LENIENT;
import static org.springframework.data.jpa.repository.query.QueryParameterSetter.ErrorHandling.*;

import jakarta.persistence.Parameter;
import jakarta.persistence.Query;
Expand All @@ -32,7 +32,6 @@

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.query.TypedParameterValue;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;

Expand Down Expand Up @@ -82,11 +81,9 @@ public void setParameter(BindableQuery query, JpaParametersParameterAccessor acc

if (temporalType != null) {

var extractedValue = valueExtractor.apply(accessor);
Object extractedValue = valueExtractor.apply(accessor);

final Date value = (extractedValue instanceof TypedParameterValue<?> typedParameterValue)
? (Date) typedParameterValue.getValue()
: (Date) extractedValue;
final Date value = accessor.extractDate(extractedValue);

// One would think we can simply use parameter to identify the parameter we want to set.
// But that does not work with list valued parameters. At least Hibernate tries to bind them by name.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,16 @@
*/
package org.springframework.data.jpa.repository.query;

import static java.util.Arrays.*;
import static jakarta.persistence.TemporalType.*;
import static java.util.Arrays.*;
import static org.mockito.ArgumentMatchers.*;
import static org.mockito.Mockito.*;
import static org.springframework.data.jpa.repository.query.QueryParameterSetter.ErrorHandling.*;

import jakarta.persistence.Parameter;
import jakarta.persistence.Query;
import jakarta.persistence.TemporalType;
import jakarta.persistence.criteria.ParameterExpression;
import lombok.Value;

import java.util.Arrays;
Expand All @@ -29,11 +33,6 @@
import java.util.List;
import java.util.function.Function;

import jakarta.persistence.Parameter;
import jakarta.persistence.Query;
import jakarta.persistence.TemporalType;
import jakarta.persistence.criteria.ParameterExpression;

import org.assertj.core.api.SoftAssertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -65,7 +64,11 @@ class NamedOrIndexedQueryParameterSetterUnitTests {
void before() {

JpaParametersParameterAccessor accessor = mock(JpaParametersParameterAccessor.class);
when(accessor.getValues()).thenReturn(new Object[] { new Date() });

Date testDate = new Date();

when(accessor.getValues()).thenReturn(new Object[] { testDate });
when(accessor.extractDate(testDate)).thenReturn(testDate);

this.methodArguments = accessor;
}
Expand Down