我有四个通过独特关系相关的 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 数据,而不返回首选项和限制数据。
我尝试过 Spring Data 的 List<ShiftDate> 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");
仍然无法工作。
谁能告诉我出了什么问题吗?
最佳答案
您还需要返回关系,这样 SDN 将填充您的子对象。
您的返回条款应如下所示:
RETURN shiftDate, has_preference, preference, has_restriction, restriction
关于Spring Data Neo4j @Query 不返回子节点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48991744/