Repository query define date input parameter format or match the configured one [DATAES-614] #1187
Labels
status: declined
A suggestion or change that we don't feel we should currently apply
type: bug
A general bug
spring-projects-issues commentedJul 22, 2019
Stefano Ghio opened DATAES-614 and commented
I am using Elasticsearch 6.5.3 and Spring Boot 2.1.6 and spring-data-elasticsearch 3.2.0.M1.
I have defined the Elasticsearch configuration to use Jackson JavaTimeModule as:
And I have defined a repository with a method as:
{{
}}List<AccountDateRollSchedule> findAllByNextRollDateTimeLessThanEqual(final LocalDateTime dateTime);
{{}}
{{
}}And the POJO AccountDateRollSchedule defines that field as:
{{
}}@Field(type = FieldType.Date, format = DateFormat.date_hour_minute) @DateTimeFormat(pattern = "yyyy-MM-dd'T'HH:mm")
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd'T'HH:mm") private LocalDateTime nextRollDateTime;
{{}}
{{}}
I see my index properly has that field created as declared and expected:
{{
}}"nextRollDateTime": { "type": "date", "format": "date_hour_minute"
}{{}}
{{
}}Also querying the index returns the field formatted as expected:
{{
}}"nextRollDateTime" : "2019-06-27T13:34"
{{}}
Passing any LocalDateTime input to the method does NOT respect the format defined for the field, the FULL format is always used instead. For example invoking:
{{findAllByNextRollDateTimeLessThanEqual(LocalDateTime.now(ZoneOffset.UTC).truncatedTo(ChronoUnit.MINUTES));
}}{{
}}gives me the following exception (any @DateTimeFormat or @JsonFormat annotation on the method parameter in the repository is ignored):
{{
}}{{
}}If I instead change the repository method to accept a String and pass a String formatted exactly as expected as input to it, it works no problem.
In fact, it looks like LocalDateTime is not handled at all and a String should be used instead because changing the field definition to:
Which is exactly the full format of the LocalDateTime object as a String, I still get an error:
Is it possible to somehow define the format used for the date parameter passed in input to the repository method or have Spring use the one configured on the field itself?
I would like not to wrap that method for a simple conversion like this (I did and it works), and I would also like to avoid using long type for the date field
Thanks and cheers
Affects: 3.2 RC1 (Moore)
Reference URL: https://stackoverflow.com/questions/56792706/spring-data-elasticsearch-repository-query-define-date-input-parameter-format
The text was updated successfully, but these errors were encountered: