java - 从 neo4j 返回的 NodeEntity 不包含关系或连接的节点

标签 java spring spring-boot neo4j

我正在设立一个 P.O.C.使用 Neo4j,并且技术上拥有我需要工作但希望其设置正确的一切。

作为快速概述 - 我可以创建节点和关系,并遍历图表(即返回特定市场中可用的所有功能),以便我知道这些节点/关系已创建。

但是,当我查询仅根据 ID 返回节点时,它仅返回该节点的数据 - 而不是任何关系或连接的节点,例如其可用的市场。

我在网上查看了很多地方,这些地方不仅返回了一个节点,而且还返回了后续节点 - 尽管我遵循他们正在做的事情,但我似乎无法让它与我的一起工作。

功能存储库:

    @Repository
    public interface FeatureRepository<T extends Feature> extends Neo4jRepository<T, Long> {
    ...
    }

颜色存储库:

    @Repository
    public interface ColourRepository extends FeatureRepository<Colour>{
        @Query("CREATE(feat:Colour:Feature {marketingDesc:{marketing}, engineeringDesc:{engineering}, code:{code}})")
        Colour createColour(@Param("marketing") String marketingDesc, @Param("engineering") String engineeringDesc, @Param("code") String code);

        @Query("MATCH (c:Colour {code:{colourCode}}) MATCH (c)-[:AVAILABLE_IN]->(market) RETURN c AS colour, COLLECT(market) AS markets")
        Colour getColourByCode(@Param("colourCode") String colourCode);

        Colour findByCode(@Param("code") String code);
    }

功能实体:

    @NodeEntity(label = "Feature")
    @Getter
    @Setter
    @AllArgsConstructor
    @NoArgsConstructor
    public class Feature {
        @Id
        @GeneratedValue
        private Long id;
        private String marketingDesc;
        private String engineeringDesc;
        @Index(unique = true)
        private String code;

        @Relationship(type = "HAS_OPTION", direction = Relationship.INCOMING)
        private List<Option> options = new ArrayList<>();

        @Relationship(type = "AVAILABLE_IN")
        private List<Market> markets = new ArrayList<>();

        @Relationship(type = "HAS_PREREQUISITE", direction = Relationship.UNDIRECTED)
        private List<Prerequisite> prerequisites = new ArrayList<>();
    }

颜色实体:

    @AllArgsConstructor
    @NodeEntity(label = "Colour")
    public class Colour extends Feature {
    }

市场实体:

    @NodeEntity(label = "Market")
    @Getter
    @Setter
    @AllArgsConstructor
    @NoArgsConstructor
    public class Market {
        @Id
        @GeneratedValue
        private Long id;

        @Index(unique = true)
        private String code;
        private String market;

        @Relationship(type = "AVAILABLE_IN", direction = Relationship.INCOMING)
        private List<Option> features = new ArrayList<>();
    }

关系实体(将功能连接到可以购买的市场):

    @RelationshipEntity(type = "AVAILABLE_IN")
    @Getter
    @Setter
    @AllArgsConstructor
    @NoArgsConstructor
    public class Available {
        @Id
        @GeneratedValue
        private Long Id;
        private List<String> availableIn = new ArrayList<>();

        @StartNode
        private Feature feature;
        @EndNode
        private Market market;
    }

Controller :

    @RestController
    public class ConfigController {

        private final Handler configHandler;

        public ConfigController(Handler configHandler) {
            this.configHandler = configHandler;
        }

     @PostMapping(path = "/create/colour", consumes = APPLICATION_JSON_VALUE, produces = APPLICATION_JSON_VALUE)
        public SimpleResponse createColour(@RequestBody Colour request) {
            ColourService service = new ColourService(configHandler);
            Colour created = service.createColour(request);
            return SimpleResponse.builder().result("Created:", created).build();
        }

        @PostMapping(path = "/create/market", consumes = APPLICATION_JSON_VALUE, produces = APPLICATION_JSON_VALUE)
        public SimpleResponse createMarket(@RequestBody Market request) {
            MarketService service = new MarketService(configHandler);
            Market created = service.createMarket(request);
            return SimpleResponse.builder().result("Created", created).build();
        }

        @PostMapping(path = "/create/relationship/availableIn", consumes = APPLICATION_JSON_VALUE, produces = APPLICATION_JSON_VALUE)
        public SimpleResponse createAvailableInRelationship(@RequestBody OptionAvailInRequest request){
            RelationshipService service = new RelationshipService(configHandler);
            Object result = service.createAvailableInRelationship(request);
            return SimpleResponse.builder().result("Result:", result).build();
        }

        @GetMapping(path = "/colour/{code}")
        public SimpleResponse getColourByCode(@PathVariable(value = "code") String code) {
            ColourService service = new ColourService(configHandler);
            Colour colour = service.getColourByCode(code);
            return SimpleResponse.builder().result("Colour:", colour).build();
        }

        @GetMapping(path = "/features/available/{mrktCode}")
        public SimpleResponse getFeaturesInMarket(@PathVariable(value = "mrktCode") String mrktCode){
            RelationshipService service = new RelationshipService(configHandler);
            Collection<Feature> features = service.getFeaturesInMarket(mrktCode);
            return SimpleResponse.builder().result("Features:", features).build();
        }
    }

Neo4j配置文件:

    @Configuration
    @EnableNeo4jRepositories(basePackages = "package.location")
    @EnableTransactionManagement
    public class Neo4jConfig {
        @Bean
        public org.neo4j.ogm.config.Configuration configuration() {
            org.neo4j.ogm.config.Configuration configuration =
                    new org.neo4j.ogm.config.Configuration.Builder().build();


            return configuration;
        }

        @Bean
        public SessionFactory sessionFactory(org.neo4j.ogm.config.Configuration configuration) {

            return new SessionFactory(configuration,"package.location");
        }

        @Bean
        public Neo4jTransactionManager transactionManager(SessionFactory sessionFactory) {
            return new Neo4jTransactionManager(sessionFactory);
        }
    }

例如,在这里我可以创建一个颜色节点:

示例值:

{
  "code": "string",
  "engineeringDesc": "string",
  "id": 0,
  "marketingDesc": "string",
  "markets": [
    {
      "code": "string",
      "features": [
        {}
      ],
      "id": 0,
      "market": "string"
    }
  ],
  "options": [
    {}
  ],
  "prerequisites": [
    {}
  ]
}

我发送的内容:

{
  "code": "BLU",
  "engineeringDesc": "Blue engineering",
  "marketingDesc": "Blue marketing"
}

这样就成功创建了一个颜色节点:

{
  "result": {
    "Created:": {
      "id": 0,
      "marketingDesc": "Blue marketing",
      "engineeringDesc": "Blue engineering",
      "code": "BLU",
      "options": [],
      "markets": [],
      "prerequisites": []
    }
  },
  "error": null
}

我可以创建一个市场节点: 示例值:

{
  "code": "string",
  "features": [
    {}
  ],
  "id": 0,
  "market": "string"
}

我发送的内容:

{
  "code": "UB",
  "market": "England"
}

成功创建市场节点:

{
  "result": {
    "Created": {
      "id": 1,
      "code": "UB",
      "market": "England",
      "features": []
    }
  },
  "error": null
}

然后我可以在两者之间创建关系,表示该颜色在该市场上可用:

{
  "featureCode": "BLU",
  "marketCode": "UB"
}

我可以通过点击以下命令来验证是否已创建: 本地主机:8080/features/available/UB

{
  "result": {
    "Features:": [
      {
        "id": 0,
        "marketingDesc": "Blue marketing",
        "engineeringDesc": "Blue engineering",
        "code": "BLU",
        "options": [],
        "markets": [],
        "prerequisites": []
      }
    ]
  },
  "error": null
}

但是,当我返回颜色节点本身时: 本地主机:8080/颜色/BLU

{
  "result": {
    "Colour:": {
      "id": 0,
      "marketingDesc": "Blue marketing",
      "engineeringDesc": "Blue engineering",
      "code": "BLU",
      "options": [],
      "markets": [],
      "prerequisites": []
    }
  },
  "error": null
}

“市场”选项始终为空。我已经尝试过使用 Neo4j 帮助器(例如 findByCode 等)自定义查询和构建查询,我能找到的每个示例都会成功返回相关节点,但我似乎无法得到我的。

有人可以帮忙吗? 附:如果还有其他内容对您有帮助,请告诉我。几天来一直试图解决这个问题......

最佳答案

找到了这个问题的答案...

功能实体应该是:

    @Relationship(type = "AVAILABLE_IN")
    @ApiModelProperty(hidden = true)
    private Set<Available> markets = new HashSet<>();

市场实体应该是:

    @Relationship(type = "AVAILABLE_IN", direction = Relationship.INCOMING)
    @ApiModelProperty(hidden = true)
    private Set<Available> features = new HashSet<>();

这使得 JSON 特征的市场部分不再为空...

现在我遇到的问题是,两个类之间存在无限递归循环,其中一个功能显示市场,而市场显示该功能

编辑:

对于遇到此/类似问题的其他人,我找到了一个非常好的 github 资源。 GitHub neo4j ogm walkthrough

帮助很大。

关于java - 从 neo4j 返回的 NodeEntity 不包含关系或连接的节点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56884660/

相关文章:

java - 当结果为列表时,JPA @OneToMany 不起作用

spring-boot - 第一次调用时出现 ZuulException (SendErrorFilter)

java - 通过 Spring Boot 连接 mysql 时出现时区问题,相当于 Mitteleuropäische Zeit

java - 绑定(bind)模型数据查看 - Spring

java - Servlet+jQuery/Ajax - 意外的 token o

java - 在Java中解析管道分隔的字符串(管道可以转义)

spring - BeanDefinitionStoreException 读取候选组件类失败

java - Hibernate 跨数据库内连接

java - 创建 AWS lambda 以将 csv 文件从 S3 复制到 RDS MySQL

java - 如何在数据库中的 map 上绘制标记