Warm tip: This article is reproduced from serverfault.com, please click

spring boot-SDN / RX自定义查询@RelationshipProperties映射深度

(spring boot - SDN/RX Custom Query @RelationshipProperties mapping depth)

发布于 2020-12-07 14:34:53

我在SDN / RX 1.1的问题上绊脚石

        <dependency>
            <groupId>org.neo4j.springframework.data</groupId>
            <artifactId>spring-data-neo4j-rx-spring-boot-starter</artifactId>
            <version>1.1.1</version>
        </dependency>

存储库上使用自定义查询并尝试查询具有属性的多个关系时。

我有两个关系属性:

@RelationshipProperties
public class InRegionProperties {

  @Id
  @GeneratedValue
  private Long id;
  private String location;
  private int distance;

  public InRegionProperties (String location, int distance) {
    this.id=null;
    this.location = location;
    this.distance = distance;
  }

  public Long getId () {
    return id;
  }

  public String getLocation () {
    return location;
  }

  public int getDistance () {
    return distance;
  }

  public void setLocation (String location) {
    this.location = location;
  }

  public void setDistance (int distance) {
    this.distance = distance;
  }

  public void setId (Long id) {
    this.id = id;
  }
}

@RelationshipProperties
public class InWorldProperties {

  @Id
  @GeneratedValue
  private Long id;
  private String location;

  public InWorldProperties (String location) {
    this.id=null;
    this.location = location;
  }

  public Long getId () {
    return id;
  }

  public String getLocationInWorld () {
    return location;
  }

  public void setLocationInWorld (String location) {
    this.location = location;
  }

  public void setId (Long id) {
    this.id = id;
  }
}

对于两个节点,一个Region节点和一个World节点。我有一个最后一个节点,一个Lieu节点,所有这些都以这种方式关联:

(l:Lieu)-[:IN_REGION {location,distance}]->(r:Region)-[:IN_WORLD {location}]->(w:World)

然后,我在WorldRepository上使用自定义查询以获取世界中的区域,然后在Region中获取lieux。

@Query("MATCH (r:`Region`)-[:IN_WORLD]->(n:`World`) WHERE id(r)=$regionId WITH n, id(n) AS __internalNeo4jId__ "
      + "RETURN n{.description, __internalNeo4jId__: id(n), .name, __nodeLabels__: labels(n), "
      + "World_IN_WORLD_Region:[(n)<-[__relationship__:IN_WORLD]-(n_regions:Region) | n_regions{.description, __internalNeo4jId__: id(n_regions), .name, __nodeLabels__: labels(n_regions), "
      + "Region_IN_REGION_Lieu: [(n_regions)<-[__relationship__:`IN_REGION`]-(n_regions_lieux:Lieu) | n_regions_lieux{.description, __internalNeo4jId__: id(n_regions_lieux), .name, __nodeLabels__: labels(n_regions_lieux), __relationship__}], __relationship__}]"
      + "}")
  Mono<World> getByRegion(Long regionId);

这是发生问题的地方:

为了获取所有的关系ipProperties,似乎SDN / RX映射引擎仅接受关系作为关系的别名。在Cypher(Neo4j浏览器)中此查询的结果是:

{
  "name": "New Finn City",
  "description": "Ville Monde, terre de la Ligue Des Héros.",
  "__internalNeo4jId__": 61,
  "World_IN_WORLD_Region": [
    {
      "__internalNeo4jId__": 14,
      "__relationship__": {
"identity": 4,
"start": 14,
"end": 61,
"type": "IN_WORLD",
"properties": {
"location": "Sud Est"
        }
      },
      "name": "Bio Town",
      "description": "Le quartier réserve naturelle. La biosphère, en constante évolution grâce au Weillenium, y est préservée et vit en liberté protégée et surveillée.",
      "Region_IN_REGION_Lieu": [],
      "__nodeLabels__": [
        "Region"
      ]
    },
    {
      "__internalNeo4jId__": 15,
      "__relationship__": {
"identity": 3,
"start": 15,
"end": 61,
"type": "IN_WORLD",
"properties": {
"location": "Nord"
        }
      },
      "name": "Astro Town",
      "description": null,
      "Region_IN_REGION_Lieu": [],
      "__nodeLabels__": [
        "Region"
      ]
    },
    {
      "__internalNeo4jId__": 13,
      "__relationship__": {
"identity": 2,
"start": 13,
"end": 61,
"type": "IN_WORLD",
"properties": {
"location": "Est"
        }
      },
      "name": "Electro Town",
      "description": "Le quartier des savants et inventeurs, où les robots et machines alimentées en Weillenium sont omniprésentes.",
      "Region_IN_REGION_Lieu": [],
      "__nodeLabels__": [
        "Region"
      ]
    },
    {
      "__internalNeo4jId__": 12,
      "__relationship__": {
"identity": 1,
"start": 12,
"end": 61,
"type": "IN_WORLD",
"properties": {
"location": "Nord Ouest"
        }
      },
      "name": "Dark Town",
      "description": "Le quartier pauvre, rongé par la criminalité, et le traffic de Weillenium. C'est le territoire de la Ligue des Villains.",
      "Region_IN_REGION_Lieu": [],
      "__nodeLabels__": [
        "Region"
      ]
    },
    {
      "__internalNeo4jId__": 11,
      "__relationship__": {
"identity": 0,
"start": 11,
"end": 61,
"type": "IN_WORLD",
"properties": {
"location": "Sud Ouest"
        }
      },
      "name": "Atom Town",
      "description": "Le quartier central de New Finn City, où siège la Ligue des Héros. C'est le centre administratif du monde.",
      "Region_IN_REGION_Lieu": [],
      "__nodeLabels__": [
        "Region"
      ]
    }
  ],
  "__nodeLabels__": [
    "World"
  ]
}

Lieux节点不是取出由于redondant使用的关系作为别名。当在cypher上为:IN_REGION关系使用另一个别名时,我得到了获取的Lieux,但是在SDN / RX中,我得到了以下错误:

Caused by: org.neo4j.driver.exceptions.value.Uncoercible: Cannot coerce NULL to Relationship
    at org.neo4j.driver.internal.value.ValueAdapter.asRelationship(ValueAdapter.java:311)
    at org.neo4j.springframework.data.core.mapping.DefaultNeo4jConverter.createInstanceOfRelationships(DefaultNeo4jConverter.java:499)
    at org.neo4j.springframework.data.core.mapping.DefaultNeo4jConverter.lambda$populateFrom$6(DefaultNeo4jConverter.java:375)
    at org.springframework.data.mapping.model.BasicPersistentEntity.doWithAssociations(BasicPersistentEntity.java:380)
    at org.neo4j.springframework.data.core.mapping.DefaultNeo4jConverter.map(DefaultNeo4jConverter.java:288)
    at org.neo4j.springframework.data.core.mapping.DefaultNeo4jConverter.lambda$createInstanceOfRelationships$22(DefaultNeo4jConverter.java:496)
    at org.neo4j.springframework.data.core.mapping.DefaultNeo4jConverter$KnownObjects.computeIfAbsent(DefaultNeo4jConverter.java:566)
    at org.neo4j.springframework.data.core.mapping.DefaultNeo4jConverter.createInstanceOfRelationships(DefaultNeo4jConverter.java:495)
    at org.neo4j.springframework.data.core.mapping.DefaultNeo4jConverter.lambda$populateFrom$6(DefaultNeo4jConverter.java:375)
    at org.springframework.data.mapping.model.BasicPersistentEntity.doWithAssociations(BasicPersistentEntity.java:380)
    at org.neo4j.springframework.data.core.mapping.DefaultNeo4jConverter.map(DefaultNeo4jConverter.java:288)
    at org.neo4j.springframework.data.core.mapping.DefaultNeo4jConverter.read(DefaultNeo4jConverter.java:138)
    ... 48 more

映射过程似乎只接受关系作为具有要在@RelationshipProperties类中映射的属性的关系的别名。

有没有办法绕过这个问题?

谢谢大家的帮助。

Questioner
Alioune Fall
Viewed
55
meistermeier 2020-12-11 15:05:38

首先,第一件事:现在有正式的SDN 6,它是SDN / RX的后继产品。半年前,我们停止了SDN / RX上的开发,而专注于新的SDN。从版本2.4开始,SDN 6可以与Spring Boot一起使用。

例如,在过去的几个月中,我们已经解决了命名大关系的问题。

如果你真的没有更新的机会,我可以看一下。如果需要,请添加评论。