gpt4 book ai didi

spring - 将 Neo4j Server 与 Spring Data Neo4j 远程使用时不保存数据

转载 作者:行者123 更新时间:2023-12-02 07:24:13 25 4
gpt4 key购买 nike

我使用 spring-data-neo4j 创建了一个 Maven 项目。我还安装了独立的 Neo4j Server Community Edition 2.3.3。我试图将一些 Vertex 对象保存到数据库中,然后简单地检索它们以检查一切是否正常。然后,我希望能够在独立服务器中打开创建的数据库以获得更好的可视化效果。我用作依赖项:

        <dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.2.5.RELEASE</version>
</dependency>

<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-neo4j</artifactId>
<version>4.0.0.RELEASE</version>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

配置类是:

@Configuration
@ComponentScan("neo4j.example")
@EnableAutoConfiguration
@EnableNeo4jRepositories("neo4j.example.repository")

public class App extends Neo4jConfiguration {

public App() {
System.setProperty("username", "neo4j");
System.setProperty("password", "root");
}

@Override
public SessionFactory getSessionFactory() {
return new SessionFactory("neo4j.example.model");
}

@Override
@Bean
@Scope(value = "session", proxyMode = ScopedProxyMode.TARGET_CLASS)
public Session getSession() throws Exception {
return super.getSession();
}

@Override
public Neo4jServer neo4jServer() {
return new RemoteServer("http://localhost:7474");
}

public static void main(String[] args) {
SpringApplication.run(App.class, args);
}


}

我的 NodeEntity 看起来像:

@NodeEntity
public class Vertex {

private String name;
@GraphId
private Long id;

@Relationship(type = "PAIRS_WITH", direction = "UNDIRECTED")
public Set<Vertex> teammates;

public Vertex() { }

// getters, setters, equals, toString
}

存储库:

@Repository
public interface VertexRepository extends GraphRepository<Vertex> {

Vertex findByName(String name);

List<Vertex> findByTeammatesName(String name);
}

服务:

@Service
public class VertexServiceImpl implements VertexService {

@Autowired
private VertexRepository vertexRepository;

@Override
@Transactional
public Vertex create(Vertex vertex) {
return vertexRepository.save(vertex);
}

@Override
@Transactional
public Iterable<Vertex> findAll() {
return vertexRepository.findAll();
}
//....
}

然后我有一个 Controller ,它有两个简单的方法来保存顶点,然后查询数据库。

@RestController
@RequestMapping("/api/")
public class GraphController {

@Autowired
VertexService vertexService;

@RequestMapping(value = "addvertex", method = RequestMethod.GET)
public void add() {
Vertex v = new Vertex();
v.setId(1l);
v.setName("name");
Vertex v2 = new Vertex();
v2.setId(2l);

v.worksWith(v2);
vertexService.create(v);
}

@RequestMapping(value = "all", method = RequestMethod.GET)
public Iterable<Vertex> getAll() {
return vertexService.findAll();
}

}

当我将顶点保存到数据库时,没有错误。当我调用/all 时,数据库是空的。我检查了 messages.log,没有异常(exception)...最后几行是:

2016-03-26 14:25:15.716+0000 INFO  [o.n.k.i.DiagnosticsManager] Interface Microsoft Wi-Fi Direct Virtual Adapter-WFP 802.3 MAC Layer LightWeight Filter-0000:
2016-03-26 14:25:15.716+0000 INFO [o.n.k.i.DiagnosticsManager] --- INITIALIZED diagnostics END ---
2016-03-26 14:25:15.747+0000 INFO [o.n.k.i.DiagnosticsManager] --- STOPPING diagnostics START ---
2016-03-26 14:25:15.747+0000 INFO [o.n.k.i.DiagnosticsManager] --- STOPPING diagnostics END ---
2016-03-26 14:25:15.747+0000 INFO [o.n.k.i.f.GraphDatabaseFacade] Shutdown started

非常感谢任何帮助!

最佳答案

我设法解决了我的问题。配置很好,问题是我试图设置 @NodeEntity 对象的 id 属性。即使我删除 @GraphId 属性,顶点也不会保存。 This post解决了同样的问题。
In Good Relations:The Spring Data Neo4j Guide Book其中提到:“如果该字段只是简单地命名为‘id’,则无需使用 @GraphId 对其进行注释,因为 OGM 会自动使用它。”

如果有某种警告/错误消息或更明确地在文档中提到您不能 setId() 并且如果这样做,节点将不会保存在数据库中,那就太好了。希望这会节省一些人的时间!

关于spring - 将 Neo4j Server 与 Spring Data Neo4j 远程使用时不保存数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36236679/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com