保存新的关系时,关系会变得疏远。

问题描述 投票:0回答:1

早上好,我使用的是SDN-RX,我是通过spring-data-neo4j-rx-spring-boot-starter 1.0.0-beta04版本添加的,我的neo4j数据库是4.0.2企业版。我有一个 ClassificationDomain 节点类型,并定义了一个 ClassificationDomain 类型的 regionClassificationDomain,我给它附加了一个 RegionType 类型的 "Continent "节点,Continent 节点是多个区域类型的层次结构的根节点,如下所示。

"continent"->"country"->"state/province" etc.....

我现在定义了实际的区域节点,这些节点也是分层的,需要附加到相应的区域类型上,如下所示

"africa" -> "zimbabwe" -> "harare" etc

我的问题是,当我保存一个新的区域节点时,比如说我想把一个子区域附加到 "Harare "上,很多其他关系在regionType层次结构和区域层次结构上都被分离了。

"Africa" detached from RegionType

我遗漏了什么或做错了什么?

我的Repositories正在扩展ReactiveNeo4jRepository。

区域POJO


    /**
     * Parent relationship to be modeled by IS_PARENT_REGION_OF relationship
     */
    package registry.domain.geography;

    import framework.domain.BayPrincipal;
    import registry.domain.taxonomy.Classification;
    import lombok.Builder;
    import lombok.Getter;
    import lombok.Setter;
    import lombok.ToString;
    import org.neo4j.springframework.data.core.schema.Node;
    import org.neo4j.springframework.data.core.schema.Relationship;

    import java.util.ArrayList;
    import java.util.Set;

    /**
     * @author ltmutiwekuziwa
     *
     */
    @Getter
    @Setter
    @ToString
    @Node("Region")
    public class Region extends BayPrincipal {

        private int regionNumber;
        private String commonName;
        private String officialName;
        private char[] iso2Code = new char[2];
        private char[] iso3Code = new char[3];
        private ArrayList<String> boundingCoordinatess;
        private boolean isMetropolis = false;
        /**
         * Parent region maintained as set to allow for changes without losing the historical record. Only one relationship then
         * should have an open endDate for the relationship
         */
        @Relationship(type="PARENT_REGION", direction= Relationship.Direction.OUTGOING)
        private Set<Region> parentRegions;

        @Relationship(type = "REGION_CLASSIFICATION", direction = Relationship.Direction.INCOMING)
        private Set<Classification> regionClassifications;

        public Set<Region> getParentRegions() {
            return parentRegions;
        }

        public void setParentRegions(Set<Region> parentRegions) {
            this.parentRegions = parentRegions;
        }

        public Set<Classification> getRegionClassifications() {
            return regionClassifications;
        }

        public void setRegionClassifications(Set<Classification> regionClassifications) {
            this.regionClassifications = regionClassifications;
        }
    }

我的分类 POJO


    package registry.domain.taxonomy;


    import framework.domain.BayModel;
    import lombok.*;
    import org.neo4j.springframework.data.core.schema.Node;
    import org.neo4j.springframework.data.core.schema.Relationship;

    import java.time.LocalDateTime;

    @Getter
    @Setter
    @ToString
    @EqualsAndHashCode
    @Node("Classification")
    public class Classification extends BayModel {
        private String classificationName;
        private String classificationCode;
        private String url;
        private LocalDateTime startDate;
        private LocalDateTime stopDate;

        @Relationship(type = "TAXONOMY_CHILD_CLASSIFICATION", direction=Relationship.Direction.OUTGOING)
        private Classification child;

    }

区域控制器,我把进口的东西给漏掉了。


    @RestController
    @RequestMapping(value="/api/v1/region")
    public class RegionRestApi extends BayRestApi<Region, RegionRepository, RegionService> {

        private final ArrayList<String> noneIds = new ArrayList<String>(Arrays.asList("none","null", "0", ""));

        @Autowired
        ClassificationService classificationService;

        @Autowired
        public RegionRestApi(RegionService service) {
            super(service);
        }

        @PostMapping("/attach/{parent}/{classification}")
        private Mono<Region> attach(@PathVariable("parent") String parentUuid,
                                    @PathVariable("classification")String  classificationUuid,
                                    @RequestBody Region region) {
            if(!this.noneIds.contains(parentUuid)) {
                region.setParentRegions(new HashSet<Region>(
                        Arrays.asList(this.service.getRepository().findById(parentUuid).block())));
            }
            Classification cls = this.classificationService.getRepository()
                    .findById(classificationUuid).block();
            if(cls != null){
                region.setRegionClassifications(new HashSet<Classification>(
                        Arrays.asList(cls)));
            }

            return this.service.getRepository().save(region);
        }
    }

先谢谢您的帮助。

java neo4j spring-data-neo4j spring-reactive
1个回答
0
投票

新答案

region 你在 rest 控制器方法中提供的图形似乎并不包含应该被持久化的整个图形。this.service.getRepository().save(region) 调用.正如旧的答案(和文档链接)中所描述的,SDN-RX将更新整个从 region 您想要存储的区域。这也包括通过父区域(s)连接的其他区域。

一个解决方案是通过一个自定义的Cypher语句来解决这个添加(因为你只是想添加两个(?)关系.另一个解决方案是重新构建你的想法是更多的领域驱动设计,你将从一个父区域工作,而不是从一个区域到它的父区域的 "反向引用"。

老答案(可能对有类似问题的人仍有帮助)。

似乎你并没有在前期获取你想要持久化的实体的相关节点。当你持久化数据的那一刻,Spring Data Neo4j RX就会在图中存储Java模型。 关系,它需要为给定的实体存储。

由于你的例子并不清楚,我只能猜测你加载的是一个 ContinentRegion 对于SDN-RX来说,这就是需要持久化的 "真相"。

更多细节可以在这里找到 https:/neo4j.github.iosdn-rxcurrent#save

© www.soinside.com 2019 - 2024. All rights reserved.