过滤关系字段

Filter on relationship's field

我正在尝试为给定关系的字段匹配获取所有实体(我希望在结果中填写我的实体的关系)。尝试在 session.loadAll() 上使用 Filter 来过滤关系的字段,但我无法使其工作。

我的实体定义如下:

@NodeEntity    
class ClockAction {
 @Id @GeneratedValue
 private Long id;
 private String description
 private User user;
 private Office office;
}

@NodeEntity    
class User {
 @Id @GeneratedValue
 private Long id;
 private String name;
 private List<ClockAction> clockActions;
}

@NodeEntity    
class Office {
 @Id @GeneratedValue
 private Long id;
 private String name;
 private List<ClockAction> clockActions;
}

据此,我需要检索 User.id 在给定 ID 集中的所有 ClockAction 实体。

这是我的尝试:

Filter filter = Filter("id", ComparisonOperator.IN, userIds);
filter.setNestedPropertyName("user");
filter.setNestedPropertyType(User.class);
filter.setNestedRelationshipEntity(true);

return session.loadAll(ClockAction.class, filter);

这总是 returns 一个空结果。知道我做错了什么吗?

像这样使用 session.query

session.query(ClockAction.class, "MATCH p=(a:ClockAction)-[r]-() WHERE id(r) IN {ids} RETURN nodes(p), rels(p), a, r ORDER BY a.id", params) 

有效,但只有 ClockAction 的 office 字段会在结果实体中填写,用户始终为 null...

感谢任何帮助:)

先说几点:

  1. 很遗憾,目前无法过滤 id 字段,因为过滤器仅适用于属性。使用 id 函数在 cypher 中查询 Id 字段。 (id(n) != n.id)

  2. 您不是在寻找关系实体(删除filter.setNestedRelationshipEntity(true);

现在您有以下选择:

  1. 使用过滤器查询 User class 的另一个 属性。

  2. 用这样的东西改变你的密码查询:"MATCH p=(a:ClockAction)-[r]-(n) WHERE id(n) IN {ids} RETURN nodes(p), rels(p), a, r ORDER BY a.id"这些变化是基于代码片段是正确的并且User不是关系的假设。

附加信息(编辑): 如果没有定义关系,Neo4j OGM 将创建它们直接从您正在保存的节点传出。您的图形可能如下所示(ClockAction 作为根):

或者像这样(用户作为 root 有多个 ClockActions):

您没有得到 Office 因为您当前的查询路径是 (:User)-[r]-(:ClockAction) 路径中没有关于 Office.

的信息

MATCH (n:User)-[ur:CLOCK_ACTIONS]->(c:ClockAction)-[or:OFFICE]->(o:Office) WHERE id(n) IN {ids} RETURN c, n, ur, o, or 是您可以使用的非常简单的查询。它删除了以路径为中心的样式,但也加载了您需要的所有数据。 如果图表是通过 User 存储的,但这只是一个示例并且可以应用,但是数据在您的图表中看起来如此,您将不会在 ClockAction 上看到任何 User 信息因为它在没有任何提示的情况下保存 Neo4j OGM 也会期望从您要加载的 class 传出方向相关的数据。 现在有必要保留 User 示例,将 @Relationship(type="CLOCK_ACTION", direction = "INCOMING") 添加到 ClockAction class 中的 user 字段。 这将为 Neo4j OGM 提供所需的提示,以将其拥有的 User 数据放入您的 user 字段中。

我最终听从了@meistermeier 的建议,并注释了我的关系指导。

下面是我的模型实体:

@NodeEntity    
class ClockAction {
 @Id @GeneratedValue
 private Long id;
 private String description
 @Relationship(direction = Relationship.OUTGOING)
 private User user;
 @Relationship(direction = Relationship.OUTGOING)
 private Office office;
}

@NodeEntity    
class User {
 @Id @GeneratedValue
 private Long id;
 private String name;
 @Relationship(direction = Relationship.INCOMING)
 private List<ClockAction> clockActions;
}

@NodeEntity    
class Office {
 @Id @GeneratedValue
 private Long id;
 private String name;
 @Relationship(direction = Relationship.INCOMING)
 private List<ClockAction> clockActions;
}

@meistermeier 建议的查询对我不起作用,但给了我灵感,我发现它工作正常:

MATCH p((u:User)-[ur]-(c:ClockAction)-[or]-()) WHERE id(u) IN {ids} RETURN p, rels(p)