我有以下Neo4j / Cypher / Spring Data Neo4j逻辑:
@Query("MATCH (parentD:Decision)-[:CONTAINS]->(childD:Decision) WHERE id(parentD) = {parentDecisionId} RETURN childD AS decision, [ (parentD)<-[:DEFINED_BY]-(c:Criterion)<-[:VOTED_ON]-(vg:VoteGroup)-[:VOTED_FOR]->(childD) | {criterion: c, weight: vg.avgVotesWeight} ] AS weightedCriteria, [ (parentD)<-[:DEFINED_BY]-(ch:Characteristic)<-[:SET_ON]-(v:Value)-[:SET_FOR]->(childD) | {characteristic: ch, value: v.value} ] AS valuedCharacteristics")
List<DecisionMatrix> findAllDecisionMatrixs(@Param("parentDecisionId") Long parentDecisionId);
因此,此SDN 4存储库方法检索DecisionMatrix
:
@QueryResult
public class DecisionMatrix {
private Decision decision;
private List<WeightedCriterion> weightedCriteria;
private List<ValuedCharacteristic> valuedCharacteristics;
}
到目前为止一切正常。
但由于条件复杂,我需要在代码中动态构造此查询,然后使用org.neo4j.ogm.session.Session
执行:
Result queryResult = session.query(cypherQuery, parameters);
获得结果后,我必须将其转换为List<DecisionMatrix>
这是我的转换器:
@SuppressWarnings("unchecked")
public static List<DecisionMatrix> convertResultToDecisionMatrixs(Result queryResult) {
List<DecisionMatrix> decisionMatrixs = new ArrayList<>();
for (Map<String, Object> result : queryResult) {
DecisionMatrix decisionMatrix = new DecisionMatrix();
decisionMatrix.setDecision((Decision) result.get(DecisionQueryBuilder.DECISION_VARIABLE_BASE_NAME));
Object weightedCriteria = result.get("weightedCriteria");
if (weightedCriteria != null) {
decisionMatrix.setWeightedCriteria((List<WeightedCriterion>) weightedCriteria);
}
Object valuedCharacteristics = result.get("valuedCharacteristics");
if (valuedCharacteristics != null) {
decisionMatrix.setValuedCharacteristics((List<ValuedCharacteristic>) valuedCharacteristics);
}
decisionMatrixs.add(decisionMatrix);
}
return decisionMatrixs;
}
但它现在在以下行中失败:
decisionMatrix.setWeightedCriteria((List<WeightedCriterion>) weightedCriteria);
有以下例外:
java.lang.ClassCastException: [Lscala.collection.convert.Wrappers$MapWrapper; cannot be cast to java.util.List
at com.example.domain.util.OGMUtils.convertResultToDecisionMatrixs(OGMUtils.java:43)
如何正确地将我的结果转换为List<DecisionMatrix>
(如SDN 4存储库方法那样)?
已更新
我更新了我的转换器:
@SuppressWarnings("unchecked")
public static List<DecisionMatrix> convertResultToDecisionMatrixs(Result queryResult) {
List<DecisionMatrix> decisionMatrixs = new ArrayList<>();
for (Map<String, Object> result : queryResult) {
DecisionMatrix decisionMatrix = new DecisionMatrix();
decisionMatrix.setDecision((Decision) result.get(DecisionQueryBuilder.DECISION_VARIABLE_BASE_NAME));
Object weightedCriteria = result.get("weightedCriteria");
if (weightedCriteria != null) {
List<WeightedCriterion> weightedCriterions = new LinkedList<>();
MapWrapper<String, Object>[] weightedCriteriaArray = (MapWrapper<String, Object>[]) weightedCriteria;
for (int i = 0; i < weightedCriteriaArray.length; i++) {
MapWrapper<String, Object> weightedCriteriaArrayElement = (MapWrapper<String, Object>) weightedCriteriaArray[i];
Double weight = (Double) weightedCriteriaArrayElement.get("weight");
Criterion criterion = (Criterion) weightedCriteriaArrayElement.get("criterion");
weightedCriterions.add(new WeightedCriterion(criterion, weight));
}
decisionMatrix.setWeightedCriteria(weightedCriterions);
}
Object valuedCharacteristics = result.get("valuedCharacteristics");
if (valuedCharacteristics != null) {
decisionMatrix.setValuedCharacteristics((List<ValuedCharacteristic>) valuedCharacteristics);
}
decisionMatrixs.add(decisionMatrix);
}
return decisionMatrixs;
}
以下行没有失败:
Criterion criterion = (Criterion) weightedCriteriaArrayElement.get("criterion");
有以下例外:
java.lang.ClassCastException: org.neo4j.kernel.impl.core.NodeProxy cannot be cast to com.example.domain.model.entity.decision.Criterion
如何解决这个问题?是否有机会从SDN 4存储库方法内部重用某些逻辑以获得正确的结果?
例如,AFAIK之前我们使用过
neo4jOperations.convert(nodeProxy, Criterion.class);
今天如何实现?