我正在使用Neo4j
Spring-boot
。尝试运行我的测试类时出现以下错误:
java.lang.NoSuchMethodError: org.springframework.data.repository.config.RepositoryConfigurationSource.getAttribute(Ljava/lang/String;)Ljava/util/Optional;
at org.springframework.data.neo4j.repository.config.Neo4jRepositoryConfigurationExtension.postProcess(Neo4jRepositoryConfigurationExtension.java:110) ~[spring-data-neo4j-5.0.2.RELEASE.jar:5.0.2.RELEASE]
at org.springframework.data.repository.config.RepositoryConfigurationDelegate.registerRepositoriesIn(RepositoryConfigurationDelegate.java:127) ~[spring-data-commons-1.13.9.RELEASE.jar:na]
at org.springframework.data.repository.config.RepositoryBeanDefinitionRegistrarSupport.registerBeanDefinitions(RepositoryBeanDefinitionRegistrarSupport.java:83) ~[spring-data-commons-1.13.9.RELEASE.jar:na]
at org.springframework.context.annotation.ConfigurationClassBeanDefinitionReader.
Pom.xlm
<groupId>com.group</groupId>
<artifactId>app</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>my_app</name>
<description>my app desc</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.9.RELEASE</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-neo4j</artifactId>
<version>5.0.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/io.swagger/swagger-annotations -->
<dependency>
<groupId>io.swagger</groupId>
<artifactId>swagger-annotations</artifactId>
<version>1.5.17</version>
</dependency>
<!-- Mysql dependency -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<!-- JPA dependency -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
** NodeTest.java:**
/**
*
* @author Prakash Pandey 11-Dec-2017
*
*/
@RunWith(SpringRunner.class)
@SpringBootTest(classes = Neo4jConfiguration.class)
public class NodeTest {
@Autowired
private NodeRepository nodeRepository;
private static Logger log = LoggerFactory.getLogger(NodeTest.class);
@Test
public void createNodes() {
log.debug("==================== Inside createNodes ======================");
MyNodes myNodes = new MyNodes();
myNodes.setUserId(1000L);
myNodes.setName("Prakash Pandey");
myNodes.setOrganisation(Organisations.HDFC.getValue());
myNodes.setCreationDate(new Date());
myNodes.setLastUpdatedOn(new Date());
myNodes.setParent(null);
myNodes.save(zhrNodes);
log.debug("==================== Outside createNodes ======================");
}
}
** MyNodes.java:**
@NodeEntity
public class MyNodes {
@Id
@GeneratedValue
private Long id;
private Long userId;
private String name;
private String organisation;
private Date creationDate;
private Date lastUpdatedOn;
@Relationship(type = Edges.CHILD, direction = Relationship.INCOMING)
private MyNodes parent;
public MyNodes getParent() {
return parent;
}
public void setParent(MyNodes parent) {
this.parent = parent;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public Long getUserId() {
return userId;
}
public void setUserId(Long userId) {
this.userId = userId;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getOrganisation() {
return organisation;
}
public void setOrganisation(String organisation) {
this.organisation = organisation;
}
public Date getCreationDate() {
return creationDate;
}
public void setCreationDate(Date creationDate) {
this.creationDate = creationDate;
}
public Date getLastUpdatedOn() {
return lastUpdatedOn;
}
public void setLastUpdatedOn(Date lastUpdatedOn) {
this.lastUpdatedOn = lastUpdatedOn;
}
}
NodeRepository.java:
public interface NodeRepository extends Neo4jRepository<MyNodes, Long> {
}
Neo4jConfiguration.java:
@Configuration
@ComponentScan(basePackages = "com.graph")
@EnableNeo4jRepositories(basePackages = "com.graph")
@EnableTransactionManagement
public class Neo4jConfiguration {
@Bean
public SessionFactory sessionFactory() {
// with domain entity base package(s)
return new SessionFactory(configuration(), "com.graph");
}
@Bean
public org.neo4j.ogm.config.Configuration configuration() {
org.neo4j.ogm.config.Configuration configuration = new org.neo4j.ogm.config.Configuration();
configuration.driverConfiguration().setURI("bolt://localhost").setCredentials("neo4j", "neo4j");
return configuration;
}
@Bean
public Neo4jTransactionManager transactionManager() {
return new Neo4jTransactionManager(sessionFactory());
}
}
修改1:
正如Nelson Christos
在他的回答中所建议的,当使用以下依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-neo4j</artifactId>
</dependency>
我收到错误:
Caused by: org.neo4j.ogm.exception.ServiceNotFoundException: Could not load driver: org.neo4j.ogm.drivers.bolt.driver.BoltDriver.
at org.neo4j.ogm.service.DriverService.load(DriverService.java:57) ~[neo4j-ogm-api-2.1.5.jar:na]
at org.neo4j.ogm.service.DriverService.load(DriverService.java:69) ~[neo4j-ogm-api-2.1.5.jar:na]
at org.neo4j.ogm.service.Components.loadDriver(Components.java:158) ~[neo4j-ogm-api-2.1.5.jar:na]
at org.neo4j.ogm.service.Components.driver(Components.java:104) ~[neo4j-ogm-api-2.1.5.jar:na]
at org.neo4j.ogm.session.SessionFactory.<init>(SessionFactory.java:44) ~[neo4j-ogm-core-2.1.5.jar:na]
at org.neo4j.ogm.session.SessionFactory.<init>(SessionFactory.java:93) ~[neo4j-ogm-core-2.1.5.jar:na]
at com.hdfc.hierarchymanagement.graph.Neo4jConfiguration.sessionFactory(Neo4jConfiguration.java:24) ~[test-classes/:na]
at com.hdfc.hierarchymanagement.graph.Neo4jConfiguration$$EnhancerBySpringCGLIB$$1a58db7d.CGLIB$sessionFactory$0(<generated>) ~[test-classes/:na]
at com.hdfc.hierarchymanagement.graph.Neo4jConfiguration$$EnhancerBySpringCGLIB$$1a58db7d$$FastClassBySpringCGLIB$$9ef0ff91.invoke(<generated>) ~[test-classes/:na]
at org.springframework.cglib.proxy.MethodProxy.invokeSuper(MethodProxy.java:228) ~[spring-core-4.3.13.RELEASE.jar:4.3.13.RELEASE]
at org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor.intercept(ConfigurationClassEnhancer.java:358) ~[spring-context-4.3.13.RELEASE.jar:4.3.13.RELEASE]
at com.hdfc.hierarchymanagement.graph.Neo4jConfiguration$$EnhancerBySpringCGLIB$$1a58db7d.sessionFactory(<generated>) ~[test-classes/:na]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_151]
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_151]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_151]
at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_151]
at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:162) ~[spring-beans-4.3.13.RELEASE.jar:4.3.13.RELEASE]
... 43 common frames omitted
编辑2:
正如** Synch在下面的回答中所建议的,**在这里更新完整的堆栈跟踪:
Caused by: java.lang.AbstractMethodError: org.neo4j.ogm.drivers.bolt.driver.BoltDriver.newTransaction(Lorg/neo4j/ogm/transaction/Transaction$Type;Ljava/lang/String;)Lorg/neo4j/ogm/transaction/Transaction;
at org.neo4j.ogm.session.transaction.DefaultTransactionManager.openTransaction(DefaultTransactionManager.java:71) ~[neo4j-ogm-core-2.1.5.jar:na]
at org.neo4j.ogm.session.transaction.DefaultTransactionManager.openTransaction(DefaultTransactionManager.java:57) ~[neo4j-ogm-core-2.1.5.jar:na]
at org.neo4j.ogm.session.delegates.TransactionsDelegate.beginTransaction(TransactionsDelegate.java:36) ~[neo4j-ogm-core-2.1.5.jar:na]
at org.neo4j.ogm.session.Neo4jSession.beginTransaction(Neo4jSession.java:463) ~[neo4j-ogm-core-2.1.5.jar:na]
at org.springframework.data.neo4j.transaction.Neo4jTransactionManager.doBegin(Neo4jTransactionManager.java:176) ~[spring-data-neo4j-4.2.9.RELEASE.jar:na]
... 25 common frames omitted
org.springframework.transaction.CannotCreateTransactionException: Could not open Neo4j Session for transaction; nested exception is java.lang.AbstractMethodError: org.neo4j.ogm.drivers.bolt.driver.BoltDriver.newTransaction(Lorg/neo4j/ogm/transaction/Transaction$Type;Ljava/lang/String;)Lorg/neo4j/ogm/transaction/Transaction;
at org.springframework.data.neo4j.transaction.Neo4jTransactionManager.doBegin(Neo4jTransactionManager.java:199)
at org.springframework.transaction.support.AbstractPlatformTransactionManager.getTransaction(AbstractPlatformTransactionManager.java:373)
at org.springframework.transaction.interceptor.TransactionAspectSupport.createTransactionIfNecessary(TransactionAspectSupport.java:461)
at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:277)
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:96)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:136)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:92)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.data.repository.core.support.SurroundingTransactionDetectorMethodInterceptor.invoke(SurroundingTransactionDetectorMethodInterceptor.java:57)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:213)
at com.sun.proxy.$Proxy78.save(Unknown Source)
at com.hdfc.hierarchymanagement.graph.NodeTest.createNodes(NodeTest.java:40)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:75)
at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:86)
at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:84)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:252)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:94)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:191)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:678)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)
答案 0 :(得分:1)
看起来像版本问题。你能尝试下面的依赖吗
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-neo4j</artifactId>
</dependency>
答案 1 :(得分:1)
Spring会自动管理其依赖项的版本,以避免兼容性问题。除非您知道自己在做什么,否则手动设置版本以覆盖此功能是不明智的。
您还缺少Spring Data Neo4j使用的驱动程序依赖项。默认情况下,它使用BoltDriver
。如果您确实希望使用此驱动程序,请将其包含在pom.xml
。
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-neo4j</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/org.neo4j/neo4j-ogm-bolt-driver-->
<dependency>
<groupId>org.neo4j</groupId>
<artifactId>neo4j-ogm-bolt-driver</artifactId>
<version>${neo4j-ogm-bolt-driver.version}</version>
</dependency>
如果您使用的是Spring 4,请使用版本2.1.5
作为螺栓驱动程序
如果您使用的是Spring 5,请使用版本3.0.2
作为螺栓驱动程序。
答案 2 :(得分:1)
spring-boot:1.5.9.RELEASE
取决于Spring 4.
spring-data-neo4j:5.0.2.RELEASE
取决于Spring 5.
您必须将neo4j依赖项降级为4.x.x
或将spring-boot升级为2.x.x
。