Issue
Project Details
I am following along with these docs Spring Data Neo4j using Kotlin and I am struggling to return the relationship from a repository query:
Database layout
I have a User
node and an Skill
node with the following relationship:
Entities
Skill
@Node
data class Skill(
@Id
val uid: UUID,
val title: String,
) {
constructor(title: String) : this(UidGenerator.makeOne(), title)
}
User
@Node
data class User(
@Id
var uid: UUID,
@Relationship(type = "DEMONSTRATES")
val demonstrates: List<Demonstrates>,
val firstName: String,
val lastName: String,
) {
constructor(demonstrates: List<Demonstrates>, firstName: String, lastName: String)
: this(UidGenerator.makeOne(), demonstrates, firstName, lastName)
}
Relationship Properties
@RelationshipProperties
data class Demonstrates(
@TargetNode
val skill: Skill,
val rating: Int,
)
The Problem I am having
When I inject the following repository and call the getDemonstrationList
I am getting the error that follows.
@Repository
interface MyRepository : Neo4jRepository<Skill, UUID> {
@Query("""
MATCH (:User)-[d:DEMONSTRATES]->(:Skill) RETURN d
""")
fun getDemonstrationList(): List<Demonstrates>
}
Caused by: org.springframework.data.neo4j.core.mapping.NoRootNodeMappingException: Could not find mappable nodes or relationships inside Record<{d: relationship<4>}> for org.springframework.data.neo4j.core.mapping.DefaultNeo4jPersistentEntity@750a04ec
at org.springframework.data.neo4j.core.mapping.DefaultNeo4jEntityConverter.read(DefaultNeo4jEntityConverter.java:106) ~[spring-data-neo4j-6.0.2.jar:6.0.2]
at org.springframework.data.neo4j.core.mapping.DefaultNeo4jEntityConverter.read(DefaultNeo4jEntityConverter.java:67) ~[spring-data-neo4j-6.0.2.jar:6.0.2]
at org.springframework.data.neo4j.core.mapping.Schema.lambda$getRequiredMappingFunctionFor$0(Schema.java:96) ~[spring-data-neo4j-6.0.2.jar:6.0.2]
at org.springframework.data.neo4j.repository.query.Neo4jQuerySupport.lambda$getMappingFunction$0(Neo4jQuerySupport.java:105) ~[spring-data-neo4j-6.0.2.jar:6.0.2]
at org.springframework.data.neo4j.core.PreparedQuery$AggregatingMappingFunction.apply(PreparedQuery.java:218) ~[spring-data-neo4j-6.0.2.jar:6.0.2]
at org.springframework.data.neo4j.core.PreparedQuery$AggregatingMappingFunction.apply(PreparedQuery.java:154) ~[spring-data-neo4j-6.0.2.jar:6.0.2]
at org.springframework.data.neo4j.core.DelegatingMappingFunctionWithNullCheck.apply(DelegatingMappingFunctionWithNullCheck.java:45) ~[spring-data-neo4j-6.0.2.jar:6.0.2]
at org.springframework.data.neo4j.core.DelegatingMappingFunctionWithNullCheck.apply(DelegatingMappingFunctionWithNullCheck.java:35) ~[spring-data-neo4j-6.0.2.jar:6.0.2]
at org.springframework.data.neo4j.core.DefaultNeo4jClient$DefaultRecordFetchSpec.lambda$partialMappingFunction$0(DefaultNeo4jClient.java:325) ~[spring-data-neo4j-6.0.2.jar:6.0.2]
at java.base/java.util.stream.ReferencePipeline$3$1.accept(ReferencePipeline.java:195) ~[na:na]
at java.base/java.util.Iterator.forEachRemaining(Iterator.java:133) ~[na:na]
at java.base/java.util.Spliterators$IteratorSpliterator.forEachRemaining(Spliterators.java:1801) ~[na:na]
at java.base/java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:484) ~[na:na]
at java.base/java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:474) ~[na:na]
at java.base/java.util.stream.ReduceOps$ReduceOp.evaluateSequential(ReduceOps.java:913) ~[na:na]
at java.base/java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234) ~[na:na]
at java.base/java.util.stream.ReferencePipeline.collect(ReferencePipeline.java:578) ~[na:na]
at org.springframework.data.neo4j.core.DefaultNeo4jClient$DefaultRecordFetchSpec.all(DefaultNeo4jClient.java:312) ~[spring-data-neo4j-6.0.2.jar:6.0.2]
at org.springframework.data.neo4j.core.Neo4jTemplate$DefaultExecutableQuery.getResults(Neo4jTemplate.java:575) ~[spring-data-neo4j-6.0.2.jar:6.0.2]
at org.springframework.data.neo4j.repository.query.Neo4jQueryExecution$DefaultQueryExecution.execute(Neo4jQueryExecution.java:51) ~[spring-data-neo4j-6.0.2.jar:6.0.2]
at org.springframework.data.neo4j.repository.query.AbstractNeo4jQuery.execute(AbstractNeo4jQuery.java:85) ~[spring-data-neo4j-6.0.2.jar:6.0.2]
at org.springframework.data.repository.core.support.RepositoryMethodInvoker.doInvoke(RepositoryMethodInvoker.java:137) ~[spring-data-commons-2.4.2.jar:2.4.2]
at org.springframework.data.repository.core.support.RepositoryMethodInvoker.invoke(RepositoryMethodInvoker.java:121) ~[spring-data-commons-2.4.2.jar:2.4.2]
at org.springframework.data.repository.core.support.QueryExecutorMethodInterceptor.doInvoke(QueryExecutorMethodInterceptor.java:152) ~[spring-data-commons-2.4.2.jar:2.4.2]
at org.springframework.data.repository.core.support.QueryExecutorMethodInterceptor.invoke(QueryExecutorMethodInterceptor.java:131) ~[spring-data-commons-2.4.2.jar:2.4.2]
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186) ~[spring-aop-5.3.2.jar:5.3.2]
at org.springframework.data.projection.DefaultMethodInvokingMethodInterceptor.invoke(DefaultMethodInvokingMethodInterceptor.java:80) ~[spring-data-commons-2.4.2.jar:2.4.2]
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186) ~[spring-aop-5.3.2.jar:5.3.2]
at org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:123) ~[spring-tx-5.3.2.jar:5.3.2]
at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:388) ~[spring-tx-5.3.2.jar:5.3.2]
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:119) ~[spring-tx-5.3.2.jar:5.3.2]
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186) ~[spring-aop-5.3.2.jar:5.3.2]
at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:137) ~[spring-tx-5.3.2.jar:5.3.2]
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186) ~[spring-aop-5.3.2.jar:5.3.2]
at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:97) ~[spring-aop-5.3.2.jar:5.3.2]
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186) ~[spring-aop-5.3.2.jar:5.3.2]
at org.springframework.data.repository.core.support.MethodInvocationValidator.invoke(MethodInvocationValidator.java:98) ~[spring-data-commons-2.4.2.jar:2.4.2]
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186) ~[spring-aop-5.3.2.jar:5.3.2]
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:215) ~[spring-aop-5.3.2.jar:5.3.2]
at com.sun.proxy.$Proxy68.getDemonstrationIds(Unknown Source) ~[na:na]
at com.ntr.neo4j.Neo4jApplication.run(Neo4jApplication.kt:44) ~[classes/:na]
at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:804) ~[spring-boot-2.4.1.jar:2.4.1]
... 5 common frames omitted
My question
I am pretty new to Kotlin and Neo4j but I do understand the Spring framework. From what I understand, the repository is not able to workout that it needs to create a Demonstrates
instance list out of the result of my query. I am interested in getting the rating
property off the relationship but the tutorial is not really clear on how I can do that. Please let me know what I am missing in order to get my query to return a list of Demonstrates
?
Solution
Starting with Spring Data Neo4j 6, relationship cannot be top-level entities anymore.
As a consequence, your repository methods must return Skill
, SomeCollection<Skill>
(where SomeCollection
can be a List
, Set
, Flux
, Page
etc).
You can also return projections based on Skill
, but not @RelationshipProperties
-annotated classes.
Answered By - fbiville
Answer Checked By - Mary Flores (JavaFixing Volunteer)