Spring Data Rest with neo4j:如何删除关系



我正在尝试创建一个示例,如何使用 spring 数据休息服务在 neo4j 中删除关系。您可以使用neo4j-movies-example进行测试。

如果我得到关于人 1 的信息,我看到有一部电影

curl -s http://localhost:8080/persons/1
{
"name" : "Keanu Reeves",
"born" : 1964,
"_links" : {
"self" : {
"href" : "http://localhost:8080/persons/1"
},
"person" : {
"href" : "http://localhost:8080/persons/1"
},
"movies" : {
"href" : "http://localhost:8080/persons/1/movies"
}
}
}

更改实体后,仍然存在关系:

curl -s -X PUT -H "Content-Type:application/json" -d '{ "name" : "Keanu Reeves", "born":1964, "movies": [] }' http://localhost:8080/persons/1
{
"name" : "Keanu Reeves",
"born" : 1964,
"_links" : {
"self" : {
"href" : "http://localhost:8080/persons/1"
},
"person" : {
"href" : "http://localhost:8080/persons/1"
},
"movies" : {
"href" : "http://localhost:8080/persons/1/movies"
}
}
}

即使curl -s -X DELETE http://localhost:8080/persons/1/movies也没有效果。那么如何在 neo4j 上删除带有 spring-data-rest 的关系呢?

[更新 1] 我试图追踪它,但以此问题结束 ->修复,是哈希集和哈希代码方法的问题。

[更新 2] 创建了一个示例,表明 Neo4j-OGM 工作正常,但 Spring 数据会制造麻烦。 -> 已修复 - 问题是缺少@Transactional,因此每次调用存储库时,都会创建一个新的事务和会话。对于更新,加载和保存必须位于同一事务中。

[更新3] 修复其他问题后,我能够理解问题。在 PATCH-Request 中,对象加载到与 save 方法不同的会话中。行为如下:对象在session[412]中加载,对象以预期的方式作,然后对象被保存在session[417]中。

有一个设置可以在 Spring Boot 应用程序中启用 OpenSessionInViewInterceptor。

spring.data.neo4j.open-in-view=true

这仅适用于常规控制器,不适用于 Spring Data REST 资源。

您可以提供以下配置来启用 Spring Data REST 的拦截器(在任何@Configuration类中):

@Bean
public OpenSessionInViewInterceptor openSessionInViewInterceptor() {
return new OpenSessionInViewInterceptor();
}
@Bean
public MappedInterceptor myMappedInterceptor() {
return new MappedInterceptor(new String[]{"/**"}, openSessionInViewInterceptor());
}

最新更新