[JAVA-3143] Custom MongoConverters not loaded when using @Query Annotations Created: 19/Jan/19  Updated: 24/Jan/19  Resolved: 24/Jan/19

Status: Closed
Project: Java Driver
Component/s: Codecs
Affects Version/s: 3.9.1
Fix Version/s: None

Type: Bug Priority: Critical - P2
Reporter: Sherif Neamatalla Assignee: Ross Lawley
Resolution: Done Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified
Environment:

Ubuntu 18.10



 Description   

When trying to use @Query Annotation on a mongoRepo, the custom codecs are not loaded, however when using normal findBy.., the codecs are correctly loaded.

Code for my Repo :

//@Repository public interface OrderRepo extends MongoRepository<Order, String> { @Query(value = "{ 'createdAt' : {$gte : ?0, $lte: ?1 }}") ArrayList<Order> findbyCreatedAtBetween(ZonedDateTime from, ZonedDateTime to); }

Code for Codecs :

// public class ZonedDateTimeToDateConverter implements Converter<ZonedDateTime, Date> {
 
    @Override
    public Date convert(ZonedDateTime zonedDateTime) {
 
        if (zonedDateTime != null)
            return Date.from(zonedDateTime.toInstant());
 
        else
            return null;
 
 
    }
}

public class DateToZonedDateTimeConverter implements Converter<Date, ZonedDateTime> { @Override public ZonedDateTime convert(Date date) { if (date != null) return ZonedDateTime.ofInstant(date.toInstant(), ZoneId.systemDefault()); else return null; } }

Mongo Configuration :

 

@Configuration
public class SpringMongoConfiguration {
 
 
    @Bean
    public MongoCustomConversions mongoCustomConversions() {
        List<Converter<?, ?>> converters = new ArrayList<>();
 
        converters.add(new ZonedDateTimeToDateConverter());
 
        converters.add(new DateToZonedDateTimeConverter());
 
        return new MongoCustomConversions(converters);
    }
 
 
}

It's also worth saying that I tried changing the query to

String> { @Query(value = "{ 'createdAt' : {$gt : ?0, $le: ?1 }}")
 

Which according to the mongo documentation is the query executed when using findByFieldBetween(), however it does not seem to be able to load the codecs, while deleting the @Query annotation does lead to the codecs being loaded.

 



 Comments   
Comment by Ross Lawley [ 22/Jan/19 ]

Hi sherifneamatalla,

This project is for the Mongo Java Driver and it looks like the issue is related to SpringData. I think you may need to go via the Spring Data help resources.

Ross

Generated at Thu Feb 08 08:58:53 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.