2014-06-24 2 views
1

QueryDSL을 사용할 때 생성 된 쿼리가 mongodb와 작동하지 않지만 jpa와 함께 작동하는 시나리오를 발견했습니다. 코드는 github에있는 프로젝트의 일부입니다.MongoDB에서 QueryDSL을 사용할 때 잘못된 쿼리가 발생했습니다

방법은 https://github.com/corneil/spring-data-demo/blob/master/src/main/java/org/springframework/data/demo/service/LocationAndDeviceServiceImpl.java

코드에 있습니다

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); 
    } 
    return 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))); 
    for (LocationUpdate loc : resultSet) { 
     result.add(loc); 
    } 
    return result; 
} 
public List<LocationUpdate> findLocations(String deviceId, Date startDate, Date endDate) { 
    return findLocationsFunctions(deviceId, startDate, endDate); 
    // return findLocationsQsl(deviceId, startDate, endDate); 
} 

는 findLocationsFunctions을 주석 때와 주석을 findLocationsQsl 당신은 문제를 야기 할 수 있습니다. 프로젝트의 일반 테스트에서 H2가 포함 된 JPA 코드가 실행됩니다. mongo 프로파일에 대한 테스트를 실행하려면 mondodb에 액세스해야합니다. database.properties 파일에 mongodb url이 포함되어 있습니다. 코드 :

./gradlew test testMongo 

나는 문제가 QueryDSL 술어가 몽고 쿼리로 변환 방법에있다 생각합니다. 처음에 mongoTemplate을했을 때. 코드 : 그것 때문에 BasicDBObject의 제한에 대한 예외를 '준

List<LocationUpdate> locations = mongoTemplate .find(
    query(where("device").is(device).and("locTime").gte(startDate) 
    .and("locTime").lte(endTime)), LocationUpdate.class); 

, 당신은 두 번째 $를 추가하고 "를 변경했습니다 수 없습니다 :

코드 :

List<LocationUpdate> locations = mongoTemplate .find(
    query(where("device").is(device).andOperator(
     where("locTime").gte(startDate), 
     where("locTime").lte(endTime))), LocationUpdate.class); 

코드 :

나는 파인더 방법을 사용할 때하면 다음과 같은 렌더링 것으로 나타났습니다

"locTime" : { 
    "$gt" : { "$date" : "2014-04-02T14:06:23.600Z"} , 
    "$lt" : { "$date" : "2014-04-02T14:06:23.931Z"} 
} 

findLocationsQsl의 코드는 locTime과 관련된 기준을 렌더링하지 않습니다. 봄 데이터 MongoDB를 1.5.0 및 QueryDSL 3.3.4

+0

locTime과 관련된 기준을 제시하지 않는다는 것은 무엇을 의미합니까? Mongo java 드라이버 호출을 디버깅 했습니까? Querydsl 경우 직렬 쿼리는 어떻게 생겼습니까? 다음과 같이 –

+0

쿼리는 기록됩니다 이 쿼리를 사용하여 찾을 수 을 봄 데이터 저장소 findByDeviceAndLocTimeBetween 사용 : { "장치": { "$ 심판": "deviceInfo", "$ ID가": { "$의 OID": "53aead0e44aed74aa839299a를" } ","$ ": {"$ date ":"2014-06- 2835 : 54 : 55.142Z "}}} findAll (locationUpdate.device.eq (device)) 및 (locationUpdate.locTime.between (startDate, endDate)))) 쿼리 사용 : {"deviceId ":" 1234-4567-6789 "} –

답변

0

테스트는 다음과 같이이 봄 데이터 MongoDB를의 Querydsl 통합의 문제처럼 보이는 Querydsl 문제 추적기에 대답했다. Spring Data는 기본 Mongodb Java API Date serialization과 다르게 날짜를 직렬화하는 것처럼 보입니다.

+0

날짜 기준이 전혀 렌더링되지 않습니다. –