关系存在于 neo4j 但不存在于 Spring @NodeEntity

Relationship Exists in neo4j but not in Spring @NodeEntity

我的域中有一个名为 Activity 的 class,如下所示

@JsonIdentityInfo(generator=ObjectIdGenerators.PropertyGenerator.class, property="id")
@NodeEntity
public class Activity {

  @GraphId
  private Long id;

  private String title;

  private String description;

  @Relationship(type = "RELATED_TO", direction = Relationship.UNDIRECTED)
  private List<Activity> relatedActivities = new ArrayList<>();

  public Activity() {
  }

  public Long getId() {
    return id;
  }

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

  public String getTitle() {
    return title;
  }

  public String getDescription() {
    return description;
  }

  public Collection<Activity> getRelatedActivities() {
    System.out.println("getting relatedActivities");
    System.out.println(relatedActivities);
    return relatedActivities;
  }

  public void addRelatedActivity(Activity activity) {
    this.relatedActivities.add(activity);
  }
}

我使用以下存储库创建关系 class:

@RepositoryRestResource(collectionResourceRel = "relationships", path = "relationships")
public interface RelationshipRepository extends GraphRepository<Relationship> {

  @Query("MATCH (a1:Activity), (a2:Activity) " +
  "WHERE a1.title = {0} AND a2.title = {1}" +
  "CREATE (a1)-[:RELATED_TO]->(a2)")
  void addRelationship(String a1Title, String a2Title);
}

我已经验证此代码可以使用 neo4j 浏览器运行,这让我可以查看现有节点和它们之间的关系。但是,当我在 Activity 对象上访问 getRelatedActivities() 时,它始终是一个空数组,即使 Activity 有其他 Activity 节点与之相关,在 neo4j 中清晰可见。

如何让 Activity 上的 relatedActivites 正确地根据其关系自动填充?

您的代码中的问题是您在此处将 "target" 定义为 Activity

@Relationship(type = "RELATED_TO", direction = Relationship.UNDIRECTED)
private List<Activity> relatedActivities = new ArrayList<>();

但是您的代码库中也有一个 RelationshipEntity class:Relationship 具有相同的类型 RELATED_TO

当 OGM 获得结果时,它会尝试匹配每个字段,但由于它将关系类型 RELATED_TO 转换为 RelationshipEntity 而不是 Activity 对象,因此它不会填充列表在 Activity class.