明确定义RelationshipEntity后,别忘了更改模型吗?

时间:2018-07-12 14:18:25

标签: spring-boot neo4j kotlin spring-data-neo4j-5

在sdn5应用程序中使用下面的域类,我可以创建人员并将其存储在neo4j中。 从数据库的角度来看,一切都很好(存在节点和关系)。

import com.fasterxml.jackson.annotation.JsonIgnore
import com.fasterxml.jackson.annotation.JsonIgnoreProperties
import com.fasterxml.jackson.annotation.JsonProperty
import com.fasterxml.jackson.annotation.ObjectIdGenerators
import org.neo4j.ogm.annotation.*

@NodeEntity(label = "Person")
data class Person(
        @Id
        @GeneratedValue
        private var id: Long?,

        @Index
        private var email: String,

        @Property var firstName: String,

        @Property var lastName: String,

        @Property var photo: String,

        @Relationship(type = "KNOWS", direction = Relationship.OUTGOING)
        @JsonIgnoreProperties("outgoing")
        private var knows: MutableList<Person>

) 

@RelationshipEntity(type = "KNOWS")
data class Knows(
        @Id
        @GeneratedValue
        val id: Long?,

        @StartNode
        private var outgoing: Person,

        @EndNode
        private var incoming: Person
) 

但是当我尝试获取实体时,对于NodeEntities来说一切正常,但对于使用以下方法的RelationshipEntities而言则失败

java.lang.NullPointerException: null
at org.neo4j.ogm.context.MappingContext.nativeId(MappingContext.java:498) ~[neo4j-ogm-core-3.1.0.jar:3.1.0]
at org.neo4j.ogm.context.MappingContext.remember(MappingContext.java:482) ~[neo4j-ogm-core-3.1.0.jar:3.1.0]
at org.neo4j.ogm.context.MappingContext.addRelationshipEntity(MappingContext.java:283) ~[neo4j-ogm-core-3.1.0.jar:3.1.0]
at org.neo4j.ogm.context.GraphEntityMapper.createRelationshipEntity(GraphEntityMapper.java:409) ~[neo4j-ogm-core-3.1.0.jar:3.1.0]
at org.neo4j.ogm.context.GraphEntityMapper.mapRelationshipEntity(GraphEntityMapper.java:351) ~[neo4j-ogm-core-3.1.0.jar:3.1.0]
at org.neo4j.ogm.context.GraphEntityMapper.mapRelationships(GraphEntityMapper.java:325) ~[neo4j-ogm-core-3.1.0.jar:3.1.0]
at org.neo4j.ogm.context.GraphEntityMapper.mapEntities(GraphEntityMapper.java:201) ~[neo4j-ogm-core-3.1.0.jar:3.1.0]
at org.neo4j.ogm.context.GraphEntityMapper.map(GraphEntityMapper.java:132) ~[neo4j-ogm-core-3.1.0.jar:3.1.0]
at org.neo4j.ogm.context.GraphEntityMapper.map(GraphEntityMapper.java:88) ~[neo4j-ogm-core-3.1.0.jar:3.1.0]

如果我删除了RelationshipEntity的显式声明,则一切正常。 我已经用Java类进行了测试……同样的问题。

我在做什么错了?

预先感谢

Github中的完整testCase 帕特里斯(Patrice)

1 个答案:

答案 0 :(得分:1)

感谢Gerrit Meier(请参阅Github),当您定义一个RelationshipEntity时,请不要忘记从模型中引用它。 在上面的示例中,我们需要编写:

@NodeEntity(label = "Person")
data class Person(
    @Id
    @GeneratedValue
    private var id: Long?,

    @Index
    private var email: String,

    @Property var firstName: String,

    @Property var lastName: String,

    @Property var photo: String,

    @Relationship(type = "KNOWS", direction = Relationship.OUTGOING)
    @JsonIgnoreProperties("outgoing")
    private var knows: MutableList<Knows>

希望这会有所帮助。