2017-01-09 1 views
0

지금 Neo4j OGM에서 열린 GitHub의 문제 https://github.com/neo4j/neo4j-ogm/issues/215에 나는이 @QueryResult 목록에 @QueryResult을 반환 org.neo4j.ogm.session.Session.query 결과 변환하기 위해 다음 해결 방법을 사용해야 인해 :SDN4, Neo4j OGM @QueryResult Session.query 및 Pagable

예 사이퍼 쿼리 :

MATCH (parentD)-[:CONTAINS]->(childD:Decision)-[ru:CREATED_BY]->(u:User) WHERE id(parentD) = {decisionId} OPTIONAL MATCH (childD)<-[:VOTED_FOR]-(vg:VoteGroup)-[:VOTED_ON]->(c:Criterion) WHERE id(c) IN {criteriaIds} WITH childD, ru, u, vg.avgVotesWeight as weight OPTIONAL MATCH (childD)<-[:SET_FOR]->(sortValue375:Value)-[:SET_ON]->(sortCharacteristic375:Characteristic) WHERE id(sortCharacteristic375) = 375 RETURN childD AS decision, ru, u, toFloat(sum(weight)) as weight, sortValue375 ORDER BY weight DESC, sortValue375.value DESC, childD.createDate DESC SKIP 0 LIMIT 1000 

해결 방법 : 이제 오른쪽

@QueryResult 
public class WeightedDecision { 

    private Decision decision; 

    private Double weight; 

... 
} 

Result queryResult = session.query(characteristicCypher, parameters); 

List<WeightedDecision> weightedDecisions = new ArrayList<>(); 

for (Map<String, Object> result : queryResult) { 
    WeightedDecision weightedDecision = new WeightedDecision(); 
    weightedDecision.setDecision((Decision) result.get("decision")); 
    weightedDecision.setWeight((double) result.get("weight")); 
    weightedDecisions.add(weightedDecision); 
} 

에서 대신 반환해야합니다. Page<WeightedDecision>

어떻게 할 수 있습니까? queryResult를 Page<WeightedDecision>

으로 변환하려면 코드를 변경하십시오. 또한이 로직에 countQuery을 어떻게 제공합니까?

답변

1

이 게시물을 참조하면 Conversion of List to Page in Springorg.springframework.data.domain.PageImpl 구현을 사용하여 결과 목록에서 Page 인스턴스로 매핑 할 수 있습니다. 귀하의 경우, 목록에 결과를 매핑 한 후에는 다음을 수행 할 수

PageRequest pageable = new PageRequest(0, 2); 
Page<WeightedDecision> page = new PageImpl<>(weightedDecisions, pageable, weightedDecisions.size()); 

배경 : 지금까지 내가이 순간에 알고는 SDN (Paging and sorting in Spring Data Neo4j 4)에서 사용자 정의 쿼리에 Pageable의를 사용할 수 없습니다. 따라서 사용자가 직접 매핑을 수행해야합니다.

두 번째 질문은 카운트 논리를 초과합니다. 필자가 생각하기에 당신은 당신이 세고 싶은 것에 따라 두 번째로 쿼리를 호출해야한다. 예를 들어 childD 노드를 계산하려면 다음과 같이하십시오.

MATCH (parentD)-[:CONTAINS]->(childD:Decision)-[ru:CREATED_BY]->(u:User) WHERE id(parentD) = {decisionId} 
OPTIONAL MATCH (childD)<-[:VOTED_FOR]-(vg:VoteGroup)-[:VOTED_ON]->(c:Criterion) WHERE id(c) IN {criteriaIds} WITH childD, ru, u, vg.avgVotesWeight as weight 
OPTIONAL MATCH (childD)<-[:SET_FOR]->(sortValue375:Value)-[:SET_ON]->(sortCharacteristic375:Characteristic) WHERE id(sortCharacteristic375) = 375 RETURN count(childD) AS result; 
+0

답장을 보내 주셔서 감사합니다. 예를 들어 새로운 PageRequest (0, 1000)를 SKIP 0 LIMIT 1000으로 변환해도 괜찮습니까? 아니면 해당 Cypher SKIP 및 LIMIT로 변환하기 전에 PageRequest.page, PageRequest.size params에 대해 수학을 수행해야합니까? – alexanoid

+0

페이지에 따라 달라집니다. 유일한 작업이 목록 대신 페이지를 반환하는 것이라면 쿼리에서 건너 뛰기 및 제한을 사용하고 전체 결과 데이터를 페이지 개체에 넣는 것이 좋습니다. –

+0

도움 주셔서 감사합니다! – alexanoid

관련 문제