我的域中有一个名为Activity的类,如下所示
@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);
}
}
我使用以下存储库类创建关系:
@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根据其关系自动填充?
答案 0 :(得分:1)
您的代码中的问题是您定义了&#34; target&#34;在这里Activity
@Relationship(type = "RELATED_TO", direction = Relationship.UNDIRECTED)
private List<Activity> relatedActivities = new ArrayList<>();
但您的代码库中还有一个RelationshipEntity
类:Relationship
类型为RELATED_TO
。
当OGM获得结果时,它会尝试匹配每个字段,但由于它将关系类型RELATED_TO
转换为RelationshipEntity
而不是Activity
对象,因此它不会填充列表中的Activity
班。