简体   繁体   中英

same RelationshipEntity between multiple type of nodes Neo4j OGM

Is there a way to map same @RelationshipEntity between different kinds of nodes, in neo4j OGM.

For example lets consider 3 Node Entities:

    @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;

}

I want to map all the entities(State,City & Branch) with the same @RelationEntity say CONTAINS :

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

    @StartNode
    StateEntity start;

    @EndNode
    CityEntity end;



}

But OGM only allows me to add a relationship between state and city, how should i write OGM for creating same RelationshipEntity between multiple type of nodes?

Or do i need to make multiple copies of this RelationshipEntity .?.

Yes, this is possible with Neo4j OGM. If you have multiple relationships of the same type in one class OGM will look at the target type and distinguish between them.

Here is the doc section I am referring to https://neo4j.com/docs/ogm-manual/3.1/reference/#reference:annotating-entities:relationship:type-discrimination

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM