Invalid Query when using QueryDSL with MongoDB -
i have found scenario query produced when using querydsl not work mongodb work jpa. code part of project on github.
the method in https://github.com/corneil/spring-data-demo/blob/master/src/main/java/org/springframework/data/demo/service/locationanddeviceserviceimpl.java
code:
private list<locationupdate> findlocationsfunctions(string deviceid, date startdate, date enddate) { logger.info("findlocations:" + deviceid + "," + startdate + "," + enddate); deviceinfo device = deviceinforepository.findbydeviceid(deviceid); if (device == null) { throw new dataretrievalfailureexception("deviceinfo:" + deviceid); } homecoming locationupdaterepository.findbydeviceandloctimebetween(device, startdate, enddate); } private list<locationupdate> findlocationsqsl(string deviceid, date startdate, date enddate) { logger.info("findlocations:" + deviceid + "," + startdate + "," + enddate); deviceinfo device = deviceinforepository.findbydeviceid(deviceid); if (device == null) { throw new dataretrievalfailureexception("deviceinfo:" + deviceid); } list<locationupdate> result = new arraylist<locationupdate>(); iterable<locationupdate> resultset = locationupdaterepository .findall(locationupdate.device.eq(device).and(locationupdate.loctime.between(startdate, enddate))); (locationupdate loc : resultset) { result.add(loc); } homecoming result; } public list<locationupdate> findlocations(string deviceid, date startdate, date enddate) { homecoming findlocationsfunctions(deviceid, startdate, enddate); // homecoming findlocationsqsl(deviceid, startdate, enddate); }
when commenting findlocationsfunctions , uncommenting findlocationsqsl able induce problem. normal tests in project execute jpa code using embedded h2. need access mondodb execute tests mongo profile.. database.properties file contains mongodb url. code:
./gradlew test testmongo
i think problem lies in how querydsl predicate converted in mongo query. when did mongotemplate . code:
list<locationupdate> locations = mongotemplate .find( query(where("device").is(device).and("loctime").gte(startdate) .and("loctime").lte(endtime)), locationupdate.class);
it gave exception 'due limitations of basicdbobject, can’t add together sec $and" , had alter to:
code:
list<locationupdate> locations = mongotemplate .find( query(where("device").is(device).andoperator( where("loctime").gte(startdate), where("loctime").lte(endtime))), locationupdate.class);
i noticed when using finder method renders following:
code:
"loctime" : { "$gt" : { "$date" : "2014-04-02t14:06:23.600z"} , "$lt" : { "$date" : "2014-04-02t14:06:23.931z"} }
the code in findlocationsqsl doesn't render criteria related loctime.
test spring info mongodb 1.5.0 , querydsl 3.3.4
as answered in querydsl issue tracker looks issue in querydsl integration of spring info mongodb. looks spring info serializes dates differently default mongodb java api date serialization.
spring-data-mongodb querydsl
No comments:
Post a Comment