以下是两个节点实体
User.java
@NodeEntity
public class User {
@GraphId
private Long id;
private String firstName;
private String lastName;
@Fetch @RelatedTo(type = "HAS_ROLE")
private Role role;
...
}
Role.java
@NodeEntity
public class Role {
@GraphId
private Long id;
private User user;
private Integer role;
...
}
如果我想让lastName的用户是“Lin”,我该如何在我的存储库中编写查询? 下面是我所做的,但它失败了......
@Query( "Match user-[:HAS_ROLE]-role where user.lastName = 'Lin' return user")
Iterable<User> getUserLin();
错误讯息是
org.springframework.dao.InvalidDataAccessResourceUsageException: Error executing statement Match user-[:HAS_ROLE]-role where user.lastName = 'Lin' return user; nested exception is org.springframework.dao.InvalidDataAccessResourceUsageException: Error executing statement Match user-[:HAS_ROLE]-role where user.lastName = 'Lin' return user; nested exception is expected 'START'
"Match user-[:HAS_ROLE]-role where user.lastName = 'Lin' return user"
^
at org.springframework.data.neo4j.support.query.CypherQueryEngine.query(CypherQueryEngine.java:56)
at org.springframework.data.neo4j.repository.query.GraphRepositoryQuery.dispatchQuery(GraphRepositoryQuery.java:91)
at org.springframework.data.neo4j.repository.query.GraphRepositoryQuery.execute(GraphRepositoryQuery.java:70)
at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.invoke(RepositoryFactorySupport.java:302)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:110)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:155)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:204)
at com.sun.proxy.$Proxy29.getUserLin(Unknown Source)
at org.krams.service.UserService.getUserLin(UserService.java:32)
at org.krams.service.UserService$$FastClassByCGLIB$$fcd59419.invoke(<generated>)
at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204)
at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:698)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:150)
at org.springframework.aop.interceptor.CustomizableTraceInterceptor.invokeUnderTrace(CustomizableTraceInterceptor.java:257)
at org.springframework.aop.interceptor.AbstractTraceInterceptor.invoke(AbstractTraceInterceptor.java:111)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:91)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:631)
at org.krams.service.UserService$$EnhancerByCGLIB$$5d141efb.getUserLin(<generated>)
at org.krams.service.test.UserServiceTester.getUserLinIterTester(UserServiceTester.java:36)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:74)
at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:83)
at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:72)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:231)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:88)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184)
at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:71)
at org.junit.runners.ParentRunner.run(ParentRunner.java:236)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:174)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
Caused by: org.springframework.dao.InvalidDataAccessResourceUsageException: Error executing statement Match user-[:HAS_ROLE]-role where user.lastName = 'Lin' return user; nested exception is expected 'START'
"Match user-[:HAS_ROLE]-role where user.lastName = 'Lin' return user"
^
at org.springframework.data.neo4j.support.query.CypherQueryEngine.parseAndExecuteQuery(CypherQueryEngine.java:67)
at org.springframework.data.neo4j.support.query.CypherQueryEngine.query(CypherQueryEngine.java:53)
... 51 more
Caused by: expected 'START'
"Match user-[:HAS_ROLE]-role where user.lastName = 'Lin' return user"
^
at org.neo4j.cypher.internal.parser.v1_6.CypherParserImpl.parse(CypherParserImpl.scala:65)
at org.neo4j.cypher.CypherParser.parse(CypherParser.scala:42)
at org.neo4j.cypher.ExecutionEngine$$anonfun$prepare$1.apply(ExecutionEngine.scala:60)
at org.neo4j.cypher.ExecutionEngine$$anonfun$prepare$1.apply(ExecutionEngine.scala:60)
at org.neo4j.cypher.internal.LRUCache.getOrElseUpdate(LRUCache.scala:31)
at org.neo4j.cypher.ExecutionEngine.prepare(ExecutionEngine.scala:60)
at org.neo4j.cypher.ExecutionEngine.execute(ExecutionEngine.scala:54)
at org.neo4j.cypher.ExecutionEngine.execute(ExecutionEngine.scala:57)
at org.neo4j.cypher.javacompat.ExecutionEngine.execute(ExecutionEngine.java:86)
at org.springframework.data.neo4j.support.query.CypherQueryEngine.parseAndExecuteQuery(CypherQueryEngine.java:65)
... 52 more
我是否应该在Spring Data Neo4j中使用“START node ... MATCH relationship ... RETURN ...”? 如果以下查询将成功:
@Query( "start user=node:User(username={0}) match user-[:HAS_ROLE]-role return user")
User getUserLin(String username);
如果删除关系并“开始”,它将会失败...为什么? {0}
中的(username={0})
表示User getUserLin(String username)
的第一个参数?
@Query( "match user where user.username='John' return user")
User getUserLin();
答案 0 :(得分:0)
您可能使用旧版本的Spring Data Neo4j MATCH ...
语法仅支持3.x