cancel
Showing results for 
Search instead for 
Did you mean: 

Join the community at Nodes 2022, our free virtual event on November 16 - 17.

How to map query result

wurangbo
Node

Hi guys,
I want to map query result to a polo object, In SDN4, I can use annotation @QueryResult .
But in SDN6, how to ?

Repository

public interface DeviceNodeRepository extends Neo4jRepository<DeviceNode, Long> {
    Query("MATCH (a:DeviceNode)-[r:CUSTOMER*]->(b:DeviceNode)   where b.deviceId={deviceId} return  a  as relatedNodes, r as nodeRelations, b as sourceNode")
    DeviceNodeConnectionResult getNodesByDeviceId(Param("deviceId") Integer deviceId);
}
public class DeviceNodeConnectionResult {
    private DeviceNode sourceNode;
    private List<DeviceNodeRelation> nodeRelations;
    private  List<DeviceNode> relatedNodes;
}

I defined an object to map, but got null

6 REPLIES 6

gerrit_meier
Neo4j
Neo4j

There is no replacement for the QueryResult in combination with repositories. Those repositories work strictly by the rules of the modeled domain classes.
For your use-case, you could for example do following:

public class DeviceNode {
// id etc.
  @Relationship("CUSTOMER")
  private List<DeviceNode> relatedNodes;
}

and a query like

MATCH (a:DeviceNode)-[r:CUSTOMER*]->(b:DeviceNode)
where b.deviceId={deviceId}
return a, collect(r), collect(b)

This will create a mapped chain of DeviceNodes which seems like an even better representation of the graph in the application.

If you strictly want to stick with a looks-like-QueryResult, more information can be found here:

wurangbo
Node

Hi guys,
And now, I can't define a custom query in DeviceNodeRepository interface. Otherwise, the following exception will be caught “ No property customQuery found for type DeviceNode!”
@Query("MATCH (a:DeviceNode)-[r:CUSTOMER*]->(b:DeviceNode) where b.deviceId={deviceId}
return collect(a) + collect(b) as relatedNodes")
List findByCustomQuery(@Param("deviceId") Integer deviceId);

it means I can only use Neo4jClient to implement custom queries. is there any code for reference?
THX

You have to use another name. The method naming pattern findBy with CustomQuery you have used would start a search for the property customQuery on the DeviceNode.
(Mostly) Everything else than findBy at the beginning of the method name will work.
Also you should not concatenate the lists but return a, collect(r) and collect(b) as separated parts of the record as mentioned above.

wurangbo
Node

hi, Ihave tried as bellow.
@Query("MATCH (a:DeviceNode)-[r:CUSTOMER*]->(b:DeviceNode) where b.deviceId={deviceId} return (collect(a) + collect(b)) as relatedNodes")
List findRelationNode(@Param("deviceId") Integer deviceId);
but get the same error.
No property findRelationNode found for type DeviceNode!

wurangbo
Node

Hi @gerrit.meier ,
I have solved this issue of finding node.
//get DeviceNode OK
Query("MATCH (a:DeviceNode)-[r:CUSTOMER*]-(b:DeviceNode) where b.deviceId=$deviceId return (collect(a) + collect(b)) as relatedNodes")
List findRelationNode(Param("deviceId") Integer deviceId);
//get deviceNodeRelation throw exception " Could not find mappable nodes or relationships inside
Query("MATCH (a:DeviceNode)-[r:CUSTOMER*]-(b:DeviceNode) where b.deviceId=$deviceId return collect(r) as nodeRelations ")
List findRelationNodeRelation(Param("deviceId") Integer deviceId);
How can I get noderelationship?

The relationships cannot exist on their own but need to be attached to the entities. In your case a and b. That's why I said that you would have to use the return pattern mentioned above.