首页 > 解决方案 > 当我在节点之间添加丰富的关系时,如何解决“关系实体不能缺少开始或结束节点”?

问题描述

我在我的 Kotlin 应用程序中使用 Java Neo4j OGM。我必须在两个节点(和)isUnit之间添加一个关系。它是具有属性的丰富关系实体。为什么我在保存时会出错?QueryUnitIsUnitvalueRelationship entity ogm.relationships.IsUnit@121de1de cannot have a missing start or end node

Neo4j 版本:3.5.3(企业版) OGM 版本:3.1.2

查询.kt:

package ogm.nodes

import ...

@NodeEntity
class Query() {
    <...>

    @Relationship(type = RelationType.IS_UNIT, direction = Relationship.OUTGOING)
    var units: MutableSet<IsUnit> = mutableSetOf()

    <...>
}

单位.kt:

package ogm.nodes

import ...

@NodeEntity
class Unit() {
    <...>

    @JsonIgnore
    @Relationship(type = RelationType.IS_UNIT, direction = Relationship.INCOMING)
    var query: IsUnit? = null

    <...>
}

IsUnit.kt:

package ogm.relationships

import ...

@RelationshipEntity(type = RelationType.IS_UNIT)
class IsUnit() {
    @Id
    @GeneratedValue
    private var id: Long? = null
    fun getId(): Long? = id

    var uuid: String? = null
    var value: Float? = null

    @StartNode
    var rate: Rate? = null
    @StartNode
    var query: Query? = null
    @EndNode
    var unit: Unit? = null
}

逻辑:

val unit = session.loadAll(
    Unit::class.java,
    Filter("uuid", ComparisonOperator.EQUALS, uuid),
    0
).first()

val isUnit = IsMUnit()
isUnit.query = query
isUnit.unit = unit
isUnit.value = v

query.units.add(isUnit)
unit.query = isUnit

session.save(query, 1)

我希望在现有节点之间建立新的关系。但我得到了:

org.neo4j.ogm.exception.core.MappingException: Relationship entity ogm.relationships.IsMeasureUnit@121de1de cannot have a missing start or end node
    at org.neo4j.ogm.context.EntityGraphMapper.haveRelationEndsChanged(EntityGraphMapper.java:546)
    at org.neo4j.ogm.context.EntityGraphMapper.getRelationshipBuilder(EntityGraphMapper.java:504)
    at org.neo4j.ogm.context.EntityGraphMapper.link(EntityGraphMapper.java:464)
    at org.neo4j.ogm.context.EntityGraphMapper.mapEntityReferences(EntityGraphMapper.java:389)
    at org.neo4j.ogm.context.EntityGraphMapper.mapEntity(EntityGraphMapper.java:237)
    at org.neo4j.ogm.context.EntityGraphMapper.map(EntityGraphMapper.java:131)
    at org.neo4j.ogm.session.delegates.SaveDelegate.save(SaveDelegate.java:79)
    at org.neo4j.ogm.session.Neo4jSession.save(Neo4jSession.java:474)
    at queryProcessor.QueryProcessor.changeQuery(QueryProcessor.kt:117)
    at queryProcessor.QueryProcessor.process(QueryProcessor.kt:24)
    at com.pathfind.ApplicationKt$module$5$3.invokeSuspend(Application.kt:74)
...

我尝试了不同深度的sessiuon.save()方法,但它仍然不起作用。

谢谢大家。

标签: javakotlinneo4jneo4j-ogm

解决方案


似乎富关系实体只能有一个@StartNode 和@EndNode。这是真的吗?如果我需要不同节点类型之间的相同关系怎么办?我要复制代码吗?


推荐阅读