多个类型的节点之间相同的RelationshipEntity Neo4j OGM

时间:2018-06-19 16:13:32

标签: graph neo4j spring-data-neo4j spring-data-neo4j-4 neo4j-ogm

在neo4j OGM中,是否有一种方法可以在不同种类的节点之间映射相同的@RelationshipEntity

例如,让我们考虑3个节点实体:

    @NodeEntity(label = "STATE")
    @JsonSnakeCase
    public class StateEntity extends BaseEntity {

        @Relationship(type = "CONTAINS")
        private CityEntity cityEntity;

        public CityEntity getCityEntity() {
            return cityEntity;
        }

        public void setCityEntity(CityEntity cityEntity) {
            this.cityEntity = cityEntity;
        }
    }

    @NodeEntity(label = "CITY")
    @JsonSnakeCase
    public class CityEntity extends BaseEntity {

        private String cityName;

        @Relationship(type = "CONTAINS",direction = Relationship.INCOMING)
        private StateEntity stateEntity;

        @Relationship(type = "CONTAINS")
        private List<BranchEntity> branchEntities;

        public String getCityName() {
            return cityName;
        }

        public void setCityName(String cityName) {
            this.cityName = cityName;
        }

        public StateEntity getStateEntity() {
            return stateEntity;
        }

        public void setStateEntity(StateEntity stateEntity) {
            this.stateEntity = stateEntity;
        }

        public List<BranchEntity> getBranchEntities() {
            return branchEntities;
        }

        public void setBranchEntities(List<BranchEntity> branchEntities) {
            this.branchEntities = branchEntities;
        }
    }

   @NodeEntity(label = "BRANCH")
@JsonSnakeCase
public class BranchEntity extends BaseEntity {


    @Relationship(type = "CONTAINS",direction = Relationship.INCOMING)
    private CityEntity cityEntity;

}

我想用相同的@RelationEntityCONTAINS映射所有实体(州,城市和分支):

@RelationshipEntity(type = "CONTAINS")
public class Contains extends BaseRelationshipEntity {

    @StartNode
    StateEntity start;

    @EndNode
    CityEntity end;



}

但是OGM只允许我在州和城市之间添加关系,我应该如何编写OGM以在多种类型的节点之间创建相同的RelationshipEntity

还是我需要为此RelationshipEntity制作多个副本??

1 个答案:

答案 0 :(得分:0)

是的,使用Neo4j OGM可以实现。如果您在一类中具有多个相同类型的关系,OGM将查看目标类型并进行区分。

这是我指的https://neo4j.com/docs/ogm-manual/3.1/reference/#reference:annotating-entities:relationship:type-discrimination

文档部分