Reputation: 111
I have modelled a simple finite state machine graph in neo4j, where the domain consists of State
entities and a FOLLOWED_BY
relationship (in cypher: (s1:State)-[r:FOLLOWS]->(s2:State)
).
Each has some properties. I need now to analyse relations among the states and don't know how the return type in
the repository interface should look like.
The (shortened) code for the entity and relationship classes (with lombok annotations):
@NodeEntity
@NoArgsConstructor
public class State {
@Getter
private String name;
@Getter
private String inputVariable;
@Getter
private String outputVariable;
}
@RelationshipEntity(type = "FOLLOWED_BY")
@NoArgsConstructor
public class Transition implements FlowTransition {
@Getter
@Property
private String guard;
@Getter
@StartNode
private State sourceState;
@Getter
@EndNode
private State targetState;
}
For some analysis which paths exists from an state to following states where the output variable of first state is used as the input variable of the following state, I need the path returned from the query. As I'm using SDN I would like to have it returned in a (custom) query from the repository.
@Repository
public interface StateRepository extends Neo4jRepository<State, Long> {
@Query("MATCH p=allShortestpaths((s1:State)-[r:FOLLOWED_BY*1..200]->(s2:State))"
+ " WHERE s1.outputVariable = s2.inputVariable AND id(s1) = {eId}"
+ " RETURN p)"
??? findAllByBpelPathRegEx(@Param("eId") String startId);
}
My question is: what class should I use as the return type to get the path object? EntityPath
or EndResult
doesn't seem to exists anymore in SDN5(maybe 4 also), so what to take? Maybe projections, but should they look like?
Upvotes: 1
Views: 937
Reputation: 111
From this question and answers How do I query for paths in spring data neo4j 4? :
EntityPath
isn't supported since SDN 4 and you should use Iterable<Map<String, Object>>
as return type (btw: List<Map<String, Object>>
works either). The keys of the Map<String, Object>
are the names of variables which you return in your Cypher query (in the example it's p
from RETURN p
).
BTW: It's maybe better you return RETURN nodes(p) AS nodes, relationships(p)
(map-keys: nodes
, relationships(p)
) as this would return your defined @NodeEntity
and @RelationshipEntity
objects and not just the simple path objects (which contains just ids (as strings) and not the node objects themselves)
Upvotes: 1
Reputation: 1738
You can take the result in an object class or you need to create a class having @QueryResult annotation collect the s1 and s2.
Upvotes: 0