【问题标题】:Spring Data Neo4j @Query not returning child nodesSpring Data Neo4j @Query 不返回子节点
【发布时间】:2018-02-26 15:23:28
【问题描述】:

我有四个通过独特关系关联的 pojo。
第一个 Employee 节点:

@NodeEntity
public class Employee {

    @GraphId
    private Long id;

    @Index(unique = true)
    private String employeeId;

    @Relationship(type = "HAS_POSITION", direction = Relationship.OUTGOING)
    private Position position;

    @Relationship(type = "HAS_SHIFT", direction = Relationship.OUTGOING)
    private List<ShiftDate> shiftDates = new ArrayList<>();

    @Relationship(type = "RESTRICTION_OF_EMPLOYEE", direction = Relationship.INCOMING)
    private List<Restriction> restrictions = new ArrayList<>();

    @Relationship(type = "PREFERENCE_OF_EMPLOYEE", direction = Relationship.INCOMING)
    private List<Preference> preferences = new ArrayList<>();

}

如您所见,它有四个关系。
我的 ShiftDate pojo 看起来像:

@NodeEntity

public class ShiftDate {

    @GraphId
    private Long id;

    private Date startDt;
    private Date endDt;

    private List<Date> dates = new ArrayList<>();

    @Relationship(type = "HAS_RESTRICTION", direction = Relationship.OUTGOING)
    private List<Restriction> restrictions = new ArrayList<>();

    @Relationship(type = "HAS_PREFERENCE", direction = Relationship.OUTGOING)
    private List<Preference> preferences = new ArrayList<>();
}

现在,限制和偏好:

@NodeEntity
public class Restriction {

    @GraphId
    private Long id;

    private String from;
    private String to;
    private String notes;
}

@NodeEntity
public class Preference {

    @GraphId
    private Long id;

    private String from;
    private String to;
    private String notes;
}

问题是当我尝试从 Employee 节点获取 ShiftDate 详细信息时,它只返回 ShiftDate 数据,而不是 Preference 和 Restriction 数据。

我尝试过 Spring Data 的 List&lt;ShiftDate&gt; getShiftDatesByEmployeeId(String employeeId) 方法,但不起作用。

试过了

@Query("MATCH (employee:Employee{employeeId:{0}})-[has_shift:HAS_SHIFT]->(shiftDate:ShiftDate)
RETURN shiftDate");

还是不行。尝试过像

这样的高级查询
@Query("MATCH (employee:Employee)-[has_shift:HAS_SHIFT]->(shiftDate:ShiftDate)-[has_preference:HAS_PREFERENCE]-> (preference:Preference)
WITH employee,shiftDate,preference
MATCH (shiftDate)-[has_restriction:HAS_RESTRICTION]->(restriction:Restriction) WHERE employee.employeeId={0}
RETURN shiftDate,preference,restriction");

还是不行。

谁能告诉我问题出在哪里?

【问题讨论】:

    标签: spring spring-boot graph neo4j spring-data-neo4j-4


    【解决方案1】:

    您还需要返回关系,这样 SDN 将填充您的子对象。

    您的 return 子句应如下所示:

    RETURN shiftDate, has_preference, preference, has_restriction, restriction
    

    【讨论】:

    • 这应该被标记为正确答案
    猜你喜欢
    • 2021-06-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多