Head's Up! These forums are read-only. All users and content have migrated. Please join us at community.neo4j.com.
07-20-2021 12:34 AM
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
07-20-2021 04:05 AM
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:
07-21-2021 02:01 AM
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
07-21-2021 08:08 AM
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.
07-21-2021 04:34 PM
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!
07-21-2021 06:14 PM
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?
07-21-2021 11:26 PM
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.
All the sessions of the conference are now available online