How to use LIMIT operator in the subset of a query

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;
}

Are you using a custom query? If so, can you post it? Anyway, when using a custom query you need to make sure the result results in one record and the data can be mapped to your domain objects. You can see in the first line of you error message that it is complaining about your query resulting in more than one record. The concept is discussed in the reference doc.

https://docs.spring.io/spring-data/neo4j/docs/current/reference/html/#custom-queries

The last part of your query

together with the statement

A User can visit multiple Locations and a location will have multiple NearbyLocation.

Will end in a result set with multiple records of the very same u2 but in combination with all nbr and nbl per record.
I think what you want to achieve can be done with

MATCH (u2)-[nbr:nearBy]->(nbl: NearByLocation)
RETURN u2, collect(nbr), collect(nbl);

This will result in just one record (per u2, but this was already limited to 1 in your query).