在 Sprint Neo4j 中通过@Query 加载实体的 "child" 时遇到问题
Having trouble getting Entity's "child" loaded via @Query in Sprint Neo4j
我有一个这样的实体:
@NodeEntity
public class Move {
@Id @GeneratedValue private Long id;
@Property("placement")
private String placement;
@Relationship(type="PARENT", direction=Relationship.INCOMING)
BoardPosition parent;
@Relationship("CHILD")
BoardPosition child;
[...]
当我 "load" 它是这样的:
Move the_move = m_store.findByPlacement("C1");
log.info("Move direct load: " + the_move.toString());
它工作正常。 parent
和 child
属性都具有正确的值。
但是,当我通过如下定义的查询访问它时:
public interface MoveStore extends Neo4jRepository<Move, Long> {
public Move findByPlacement(String placement);
@Query("MATCH (n:Move) RETURN n")
public List<Move> findAll();
并像这样访问:
ListIterator<Move> moves = m_store.findAll().listIterator();
while(moves.hasNext() ) {
log.info(moves.next().toString());
}
缺少 child
值(为空)。
这个实验很奇怪:
while(moves.hasNext() ) {
Move m = moves.next();
log.info(m.toString()); // This is missing m.child's value
// find the same Move, by passing in the id of this one:
m = m_store.findById(m.id).orElse(null);
log.info(m.toString()); // This has the correct value for m.child!
}
我错过了什么?如何让查询加载子项 属性?
当您使用自定义查询时,您还必须 return 关系和相关节点来填充子节点。
例如@Query("MATCH (n:Move)-[rel:CHILD]-(c:BoardPosition) RETURN n, rel, c")
将为 1:1 关系完成工作,否则需要 collect(...)
才能获得与您查询的节点相同结果 "row" 的列表。
我有一个这样的实体:
@NodeEntity
public class Move {
@Id @GeneratedValue private Long id;
@Property("placement")
private String placement;
@Relationship(type="PARENT", direction=Relationship.INCOMING)
BoardPosition parent;
@Relationship("CHILD")
BoardPosition child;
[...]
当我 "load" 它是这样的:
Move the_move = m_store.findByPlacement("C1");
log.info("Move direct load: " + the_move.toString());
它工作正常。 parent
和 child
属性都具有正确的值。
但是,当我通过如下定义的查询访问它时:
public interface MoveStore extends Neo4jRepository<Move, Long> {
public Move findByPlacement(String placement);
@Query("MATCH (n:Move) RETURN n")
public List<Move> findAll();
并像这样访问:
ListIterator<Move> moves = m_store.findAll().listIterator();
while(moves.hasNext() ) {
log.info(moves.next().toString());
}
缺少 child
值(为空)。
这个实验很奇怪:
while(moves.hasNext() ) {
Move m = moves.next();
log.info(m.toString()); // This is missing m.child's value
// find the same Move, by passing in the id of this one:
m = m_store.findById(m.id).orElse(null);
log.info(m.toString()); // This has the correct value for m.child!
}
我错过了什么?如何让查询加载子项 属性?
当您使用自定义查询时,您还必须 return 关系和相关节点来填充子节点。
例如@Query("MATCH (n:Move)-[rel:CHILD]-(c:BoardPosition) RETURN n, rel, c")
将为 1:1 关系完成工作,否则需要 collect(...)
才能获得与您查询的节点相同结果 "row" 的列表。