Also, the SDN entities, if they contain indexed properties, there should be indexes for these, which let's you target these nodes:
START n=node:League("name:*") RETURN n ORDER BY n.startDate? DESC LIMIT 1
HTH
Question
I've the League
node entity as below
@NodeEntity
class League
{
private Date startDate;
}
I want to return the most recent league for which the below cypher is working fine when executed from the shell
START n=node(*) WHERE has(n.__type__) and n.__type__='com.aravind.avl.domain.League' RETURN n ORDER BY n.startDate ASC LIMIT 1
I moved this to repository as below
public interface LeagueRepository extends GraphRepository<League>
{
@Query ("START n=node(*) RETURN n ORDER BY n.startDate DESC LIMIT 1")
League findCurrentLeague();
}
It is giving me the below error. I believe we no need to provide the spring-data-neo4j implementation details like __type__
when using the repositories. Wondering how to correctly write the query in repository without exposing the spring-data-neo4j implementation details?
JUNIT
@Test
public void findCurrentLeague() throws ParseException
{
SimpleDateFormat df = new SimpleDateFormat("dd/MM/yyyy");
League l1 = new League();
l1.setStartDate(df.parse("01/03/2013"));
l1.setEndDate(new Date());
l1.setName("in year 2013");
League l2 = new League();
l2.setStartDate(df.parse("01/03/2001"));
l2.setEndDate(df.parse("01/10/2001"));
l2.setName("in year 2001");
repo.save(l1);
repo.save(l2);
League currentLeague = repo.findCurrentLeague();
assertNotNull(currentLeague);
assertEquals("in year 2013", currentLeague.getName());
}
Error
Caused by: org.springframework.dao.InvalidDataAccessResourceUsageException: Error executing statement START n=node(*) RETURN n ORDER BY n.startDate DESC LIMIT 1; nested exception is org.neo4j.cypher.EntityNotFoundException: The property 'startDate' does not exist on Node[0]
at org.springframework.data.neo4j.support.query.CypherQueryEngine.parseAndExecuteQuery(CypherQueryEngine.java:63)
at org.springframework.data.neo4j.support.query.CypherQueryEngine.query(CypherQueryEngine.java:49)
... 39 more
Caused by: org.neo4j.cypher.EntityNotFoundException: The property 'startDate' does not exist on Node[0]
at org.neo4j.cypher.internal.commands.expressions.Property.apply(Property.scala:35)
at org.neo4j.cypher.internal.commands.expressions.Property.apply(Property.scala:29)
at org.neo4j.cypher.internal.pipes.ExtractPipe$$anonfun$createResults$1$$anonfun$apply$1.apply(ExtractPipe.scala:37)
at org.neo4j.cypher.internal.pipes.ExtractPipe$$anonfun$createResults$1$$anonfun$apply$1.apply(ExtractPipe.scala:35)
at scala.collection.immutable.Map$Map1.foreach(Map.scala:118)
at org.neo4j.cypher.internal.pipes.ExtractPipe$$anonfun$createResults$1.apply(ExtractPipe.scala:35)
at org.neo4j.cypher.internal.pipes.ExtractPipe$$anonfun$createResults$1.apply(ExtractPipe.scala:34)
at scala.collection.Iterator$$anon$19.next(Iterator.scala:335)
at org.neo4j.cypher.internal.pipes.TopPipe.createResults(TopPipe.scala:45)
at org.neo4j.cypher.internal.pipes.ColumnFilterPipe.createResults(ColumnFilterPipe.scala:37)
at org.neo4j.cypher.internal.executionplan.ExecutionPlanImpl$$anonfun$6.apply(ExecutionPlanImpl.scala:127)
at org.neo4j.cypher.internal.executionplan.ExecutionPlanImpl$$anonfun$6.apply(ExecutionPlanImpl.scala:125)
at org.neo4j.cypher.internal.executionplan.ExecutionPlanImpl.execute(ExecutionPlanImpl.scala:33)
at org.neo4j.cypher.ExecutionEngine.execute(ExecutionEngine.scala:59)
at org.neo4j.cypher.ExecutionEngine.execute(ExecutionEngine.scala:63)
at org.neo4j.cypher.javacompat.ExecutionEngine.execute(ExecutionEngine.java:79)
at org.springframework.data.neo4j.support.query.CypherQueryEngine.parseAndExecuteQuery(CypherQueryEngine.java:61)
... 40 more
Caused by: org.neo4j.graphdb.NotFoundException: 'startDate' property not found for NodeImpl#0.
Solution
Also, the SDN entities, if they contain indexed properties, there should be indexes for these, which let's you target these nodes:
START n=node:League("name:*") RETURN n ORDER BY n.startDate? DESC LIMIT 1
HTH
OTHER TIPS
Do you have the property startDate on every node? The exception indicates:
Caused by: org.neo4j.cypher.EntityNotFoundException: The property 'startDate' does not exist on Node[0]
at org.neo4j.cypher.internal.commands.expressions.Property.apply(Property.scala:35)
and it looks like it's complaining about the reference node.
Since you're starting at all nodes using node(*) it's probably picking up the reference node. If you don't need it then you might be able to get rid of it, or, if you don't have startDate anyway on all nodes, maybe modify your query to:
START n=node(*) RETURN n ORDER BY n.startDate? DESC LIMIT 1