我无法将 Spring Data Neo4j 和嵌入式 Neo4j 服务器组合到一个应用程序中。我单独使用它们,但无法让它们一起工作。 另外,我还没有找到有关如何在任何地方一起使用它们的指南。
可以在这里找到 Spring-data-neo4j 的 MRE 的良好基础:gs-accessing-data-neo4j。
有关如何运行嵌入式 Neo4J 服务器的线索可以在这里找到:
结合这些资源,我创建了这个项目:
./pom.xml
:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.1.2</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>accessing-data-neo4j-complete</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>accessing-data-neo4j-complete</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>17</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-neo4j</artifactId>
</dependency>
<dependency>
<groupId>org.neo4j</groupId>
<artifactId>neo4j</artifactId>
<version>5.10.0</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
.\src\main\java\myapp\AccessingDataNeo4jApplication.java
:
package myapp;
import java.io.File;
import java.time.Duration;
import java.util.Arrays;
import java.util.List;
import org.neo4j.configuration.GraphDatabaseSettings;
import org.neo4j.cypherdsl.core.renderer.Configuration;
import org.neo4j.cypherdsl.core.renderer.Dialect;
import org.neo4j.dbms.api.DatabaseManagementService;
import org.neo4j.dbms.api.DatabaseManagementServiceBuilder;
import org.neo4j.graphdb.GraphDatabaseService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.data.neo4j.repository.config.EnableNeo4jRepositories;
import static org.neo4j.configuration.GraphDatabaseSettings.DEFAULT_DATABASE_NAME;
@SpringBootApplication
@EnableNeo4jRepositories
public class AccessingDataNeo4jApplication {
private final static Logger log = LoggerFactory.getLogger(AccessingDataNeo4jApplication.class);
public static void main(String[] args) throws Exception {
SpringApplication.run(AccessingDataNeo4jApplication.class, args);
System.exit(0);
}
@Bean
public Configuration cypherDslConfiguration() {
return Configuration.newConfig().withDialect(Dialect.NEO4J_5).build();
}
@Bean
public DatabaseManagementService databaseManagementService() {
DatabaseManagementService managementService = new
DatabaseManagementServiceBuilder(new File("target/mydb").toPath())
.setConfig(GraphDatabaseSettings.transaction_timeout, Duration.ofSeconds( 60 ) )
.build();
log.info("managementService created: {}",managementService);
registerShutdownHook(managementService);
return managementService;
}
@Bean
public GraphDatabaseService graphDatabaseService(DatabaseManagementService managementService) {
// managementService.createDatabase("mydb");
GraphDatabaseService graphDb = managementService.database(DEFAULT_DATABASE_NAME);
log.info("graphDb.isAvailable: {}",graphDb.isAvailable()); //true
return graphDb;
}
private static void registerShutdownHook( final DatabaseManagementService managementService ) {
Runtime.getRuntime().addShutdownHook( new Thread() {
@Override
public void run() {
managementService.shutdown();
}
} );
}
@Bean
CommandLineRunner demo(PersonRepository personRepository) {
return args -> {
personRepository.deleteAll();
Person greg = new Person("Greg");
Person roy = new Person("Roy");
Person craig = new Person("Craig");
List<Person> team = Arrays.asList(greg, roy, craig);
log.info("Before linking up with Neo4j...");
team.stream().forEach(person -> log.info("\t" + person.toString()));
personRepository.save(greg);
personRepository.save(roy);
personRepository.save(craig);
greg = personRepository.findByName(greg.getName());
greg.worksWith(roy);
greg.worksWith(craig);
personRepository.save(greg);
roy = personRepository.findByName(roy.getName());
roy.worksWith(craig);
// We already know that roy works with greg
personRepository.save(roy);
// We already know craig works with roy and greg
log.info("Lookup each person by name...");
team.stream().forEach(person -> log.info(
"\t" + personRepository.findByName(person.getName()).toString()));
List<Person> teammates = personRepository.findByTeammatesName(greg.getName());
log.info("The following have Greg as a teammate...");
teammates.stream().forEach(person -> log.info("\t" + person.getName()));
};
}
}
.\src\main\java\myapp\Person.java
:
package myapp;
import java.util.*;
import java.util.stream.Collectors;
import org.springframework.data.neo4j.core.schema.*;
@Node
public class Person {
@Id @GeneratedValue
private Long id;
private String name;
private Person() {
};
public Person(String name) {
this.name = name;
}
@Relationship(type = "TEAMMATE")
public Set<Person> teammates;
public void worksWith(Person person) {
if (teammates == null) {
teammates = new HashSet<>();
}
teammates.add(person);
}
public String toString() {
return this.name + "'s teammates => "
+ Optional.ofNullable(this.teammates).orElse(
Collections.emptySet()).stream()
.map(Person::getName)
.collect(Collectors.toList());
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
.\src\main\java\myapp\PersonRepository.java
:
package myapp;
import java.util.List;
import org.springframework.data.neo4j.repository.Neo4jRepository;
public interface PersonRepository extends Neo4jRepository<Person, Long> {
Person findByName(String name);
List<Person> findByTeammatesName(String name);
}
mvn clean spring-boot:run
导致抛出这些异常:
INFO 8164 --- [ main] o.neo4j.driver.internal.DriverFactory : Direct driver instance 1068773063 created for server address localhost:7687
ERROR 8164 --- [ main] o.s.boot.SpringApplication : Application run failed
java.lang.IllegalStateException: Failed to execute CommandLineRunner
Caused by: org.springframework.transaction.TransactionSystemException: Could not open a new Neo4j session: Unable to connect to localhost:7687, ensure the database is running and that there is a working network connection to it.
Caused by: io.netty.channel.AbstractChannel$AnnotatedConnectException: Connection refused: no further information: localhost/127.0.0.1:7687
Caused by: java.net.ConnectException: Connection refused: no further information
INFO 8164 --- [ main] o.neo4j.driver.internal.InternalDriver : Closing driver instance 1068773063
INFO 8164 --- [ main] o.n.d.i.async.pool.ConnectionPoolImpl : Closing connection pool towards localhost:7687
正如 cybersam 所指出的,Bolt 连接器应该在嵌入式实例上打开。此页文档显示了如何执行此操作: https://neo4j.com/docs/java-reference/current/java-embedded/bolt/
<dependency>
<groupId>org.neo4j</groupId>
<artifactId>neo4j-bolt</artifactId>
<version>5.10.0</version>
</dependency>
DatabaseManagementService
对象,请添加配置行BoltConnector
:DatabaseManagementService managementService = new
DatabaseManagementServiceBuilder(new File("target/mydb").toPath())
.setConfig(GraphDatabaseSettings.transaction_timeout, Duration.ofSeconds( 60 ) )
.setConfig( BoltConnector.enabled, true )
.setConfig( BoltConnector.listen_address, new SocketAddress( "localhost", 7687 ) )
.build();
附注同样的问题,更详细的答案:Spring Boot嵌入neo4j用于演示目的