Skip to content

Commit

Permalink
Allow JpaParametersParameterAccessor to extract dates from parameters.
Browse files Browse the repository at this point in the history
This allows the Hibernate variant (HibernateJpaParametersParameterAccessor) to potentially unwrap TypedParameterValue.

Closes #2857
Original pull request #2859
  • Loading branch information
gregturn authored and schauder committed Mar 15, 2023
1 parent e853a7a commit cf97015
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 16 deletions.
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

0 comments on commit cf97015

Please sign in to comment.