Showing results for 
Search instead for 
Did you mean: 

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

Spring data neo4j get query result POJO

Graph Buddy


I am using neo4j in my java project


I have this query :

@Query("MATCH (node)<-[:e*]-(a:app) \n" +
	        "WHERE$nodeName \n" +
			"WITH collect(DISTINCT a.nna) as nnaList, labels(node) as labels, node \n" +
	        "RETURN {nodeName, environment: node.u_environment,nnas: nnaList, labels: labels}")
	NodeInfoDTO getNodeInfo(String nodeName);

and the NodeInfoDTO :

@FieldDefaults(level = AccessLevel.PRIVATE)
public class NodeInfoDTO {
	String nodeName;
	String environment;
	List<String> nnas = new ArrayList<>();
	List<String> labels = new ArrayList<>();

when i try this i have this error : java.util.HashMap cannot be cast to fr.enedis.oi.poulpe.enrichissement.dto.NodeInfoDTO

Is there a way to fix this or i have to get a Map and cretae my object ?

Thank you for your help.


I don't think this can work as-is.
As far as I understand, projected DTOs should be a subset and/or a combination of the related entity properties. In other words, you cannot map to an arbitrary DTO with projections.

If you need this kind of flexibility, you should rather use Neo4jTemplate or Neo4jClient directly.