spring-boot - 在两个节点上指定关系会导致 Spring Data Neo4j 中的逻辑循环映射依赖关系
问题描述
我有一个用例,我需要在两个节点类中指定关系,一个作为传入,一个作为传出。这在 SDN 6.0.2 中有效,但在尝试更新到 6.1.5 时失败。我有一个基类和两个自定义类定义为
abstract class Base(
@DateLong @CreatedDate @LastModifiedDate var updated: Date? = null,
@CreatedBy @LastModifiedBy var source: String? = null,
) {
abstract val customId: String
}
@Node
data class Foo(
@Id override val customId: String,
val name: String,
@Relationship(
type = "CONFIGURED_TO",
direction = Relationship.Direction.INCOMING
) var bars: Set<Bar> = emptySet(),
) : Base()
@Node
data class Bar(
@Id override val customId: String,
val name: String,
@Relationship(
type = "CONFIGURED_TO",
direction = Relationship.Direction.OUTGOING
) var foos: Set<Foo> = emptySet(),
) : Base()
当使用最近的 SDN 更新执行查询时,两个节点类中定义的这种关系会导致org.springframework.data.mapping.MappingException: The node with id 10 has a logical cyclic mapping dependency. Its creation caused the creation of another node that has a reference to this.
获取时我不需要获取Foo
节点Bar
,但是在更新关系时定义关系对于更简单的实现都是必需的。有没有办法在使用 Spring Data Neo4j 派生查询时不获取节点?
解决方案
一些背景:事实是它在 6.0(.2) 中也不能 100% 正确工作。Foo
如果或Bar
解决构造函数依赖关系(由数据类定义)的循环,SDN 不仅创建了一个实例,而且创建了两个实例。当然,这种行为不是故意的。
要在获取Foo
时不获取节点,Bar
您应该使用投影:https ://docs.spring.io/spring-data/neo4j/docs/current/reference/html/#projections
例如(在 Java 中)
interface BarProjection {
String getName();
}
并在存储库中
interface BarRepository extends Neo4jRepository<Bar, String> {
BarProjection findByName(String name);
}
!但这不会使您免于清理导致数据类中的鸡/蛋问题的循环定义的依赖项。
推荐阅读
- java - 通过 docker-compose 将 JAVA_OPTS 传递给 Spring Boot 应用程序
- android - 使用 Android Studio 安装 Flutter
- reactjs - 在 React 中使用 this.state 中的值的语法
- azure-devops - ARM错误的VSTS管道部署:找不到与模板文件模式匹配的任何文件
- d3.js - 使用 D3 的可拖动交互式图形元素
- jenkins - 在 Jenkins Scripted Pipeline 中跨阶段设置和引用变量
- raku - 约束多重及其用于选择它们的用途
- javascript - Hc 粘性插件 IE11 问题 - 导航栏没有全宽
- bash - BASH参数扩展时单引号行为不一致的原因?
- javascript - PHP:刷新特定页面上的每个人