Spring boot version: 3.1.3
spring-data-neo4j: 7.1.3
Here is my use case -
A User can visit multiple Locations and a location will have multiple NearbyLocation.
User ------visits------->Location-------nearBy------->NearByLocation.
I wanted to get the Location of a User he has visited recently along with NearByLocation. I wrote the following query and it is working perfectly in the Neo4j console/browser
MATCH (u1: User)-[:visit]->(u2: Location)
WHERE u1.userId = "123"
WITH u2
ORDER BY u2.createdAt DESC
LIMIT 1
MATCH (u2)-[nbr:nearBy]->(nbl: NearByLocation)
RETURN u2, nbr, nbl;
but throwing the following exception while using spring data neo4j -
org.springframework.dao.IncorrectResultSizeDataAccessException: Expected a result with a single record, but this result contains at least one more. Ensure your query returns only one record.
at org.springframework.data.neo4j.core.Neo4jTemplate$DefaultExecutableQuery.getSingleResult(Neo4jTemplate.java:1119) ~[spring-data-neo4j-7.1.3.jar:7.1.3]
at org.springframework.data.neo4j.repository.query.Neo4jQueryExecution$DefaultQueryExecution.execute(Neo4jQueryExecution.java:53) ~[spring-data-neo4j-7.1.3.jar:7.1.3]
at org.springframework.data.neo4j.repository.query.AbstractNeo4jQuery.execute(AbstractNeo4jQuery.java:93) ~[spring-data-neo4j-7.1.3.jar:7.1.3]
at org.springframework.data.repository.core.support.RepositoryMethodInvoker.doInvoke(RepositoryMethodInvoker.java:136) ~[spring-data-commons-3.1.3.jar:3.1.3]
at org.springframework.data.repository.core.support.RepositoryMethodInvoker.invoke(RepositoryMethodInvoker.java:120) ~[spring-data-commons-3.1.3.jar:3.1.3]
at org.springframework.data.repository.core.support.QueryExecutorMethodInterceptor.doInvoke(QueryExecutorMethodInterceptor.java:164) ~[spring-data-commons-3.1.3.jar:3.1.3]
at org.springframework.data.repository.core.support.QueryExecutorMethodInterceptor.invoke(QueryExecutorMethodInterceptor.java:143) ~[spring-data-commons-3.1.3.jar:3.1.3]
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:184) ~[spring-aop-6.0.11.jar:6.0.11]
at org.springframework.data.projection.DefaultMethodInvokingMethodInterceptor.invoke(DefaultMethodInvokingMethodInterceptor.java:72) ~[spring-data-commons-3.1.3.jar:3.1.3]
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:184) ~[spring-aop-6.0.11.jar:6.0.11]
at org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:123) ~[spring-tx-6.0.11.jar:6.0.11]
at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:391) ~[spring-tx-6.0.11.jar:6.0.11]
Here are the domain objects -
@Node
public class User {
@Id
private String userId;
@Relationship(type="visits", direction = Relationship.Direction.OUTGOING)
private Location location;
}
@Node
public class Location {
@Id
@GeneratedValue(generatorClass = UUIDStringGenerator.class)
private String id;
@Relationship(type="nearBy", direction = Relationship.Direction.OUTGOING)
private ArrayList<NearByLocation> nearByLocations;
}