Neo4j一对多获取数据

时间:2015-04-02 11:33:14

标签: neo4j cypher spring-data-neo4j

在我的Spring Boot / Neo4j应用程序中,我有以下实体:

@NodeEntity
public class User extends BaseEntity {

    private static final String HAS = "HAS";

    @GraphId
    private Long id;

    private String username;

    private String password;

    private String email;

    private String firstName;

    private String lastName;

    @RelatedTo(type = HAS, direction = Direction.OUTGOING)
    private Set<Role> roles = new HashSet<Role>();

....

}


@NodeEntity
public class Vote extends BaseEntity {

    private static final String VOTED_ON = "VOTED_ON";
    private final static String VOTED_FOR = "VOTED_FOR";
    private static final String CREATED_BY = "CREATED_BY";

    @GraphId
    private Long id;

    @RelatedTo(type = VOTED_FOR, direction = Direction.OUTGOING)
    private Decision decision;

    @RelatedTo(type = VOTED_ON, direction = Direction.OUTGOING)
    private Criterion criteria;

    @RelatedTo(type = CREATED_BY, direction = Direction.OUTGOING)
    private User author;

    private double weight;

    private String description;


}

我有以下SDN存储库方法:

@Query("MATCH (d:Decision)<-[:VOTED_FOR]-(v:Vote)-[:VOTED_ON]->(c:Criterion) WHERE id(d) = {decisionId} AND id(c) = {criterionId} RETURN v")
List<Vote> getVotes(@Param("decisionId") Long decisionId, @Param("criterionId") Long criterionId);

在结果中,我有一个Vote列表。

在客户端,我不仅要显示这些投票的列表,还要显示用户(作者)Id,还要显示作者username

如何根据Vote.author.usernameVote.author.id上方的查询获取,并且不从User实体关联的Vote实体获取所有其他信息?无论如何,现在我只有Vote.author.id值。

编辑:

以下查询

@Query("MATCH (d:Decision)<-[:VOTED_FOR]-(v:Vote)-[:VOTED_ON]->(c:Criterion) WHERE id(d) = {decisionId} AND id(c) = {criterionId} WITH v, d, c MATCH v-[:CREATED_BY]->(u:User) RETURN {id: v.id, weight: v.weight, description: v.description, `decision.id` : d.id, `criterion.id` : c.id, `author.id`: u.id, `author.username`: u.username} as v")
List<Vote> getVotesForDecisionOnCriterion(@Param("decisionId") Long decisionId, @Param("criterionId") Long criterionId);

不会处理以下异常:

org.springframework.dao.InvalidDataAccessApiUsageException: [Assertion failed] - entity is required; it must not be null
    at org.springframework.data.neo4j.support.ParameterCheck.notNull(ParameterCheck.java:29)
    at org.springframework.data.neo4j.support.Neo4jTemplate.projectTo(Neo4jTemplate.java:240)
    at org.springframework.data.neo4j.support.conversion.EntityResultConverter.doConvert(EntityResultConverter.java:73)
    at org.springframework.data.neo4j.conversion.DefaultConverter.convert(DefaultConverter.java:44)
    at org.springframework.data.neo4j.support.conversion.EntityResultConverter.convert(EntityResultConverter.java:165)
    at org.springframework.data.neo4j.conversion.QueryResultBuilder$1.underlyingObjectToObject(QueryResultBuilder.java:86)

2 个答案:

答案 0 :(得分:1)

尝试使用DTO(也可以是带有getter的接口):

@QueryResult
class VoteView {
   String id;
   Double weight;
   String description;
   String decisionId;  
   String criterionId;  
   String authorId;
   String authorName;
}

@Query("MATCH (d:Decision)<-[:VOTED_FOR]-(v:Vote)-[:VOTED_ON]->(c:Criterion) WHERE id(d) = {decisionId} AND id(c) = {criterionId} WITH v, d, c MATCH v-[:CREATED_BY]->(u:User) RETURN {id: v.id, weight: v.weight, description: v.description, decisionId : d.id, criterionId : c.id, authorId: u.id, authorName: u.username} as v")
List<VoteView> getVotesForDecisionOnCriterion(@Param("decisionId") Long decisionId, @Param("criterionId") Long criterionId);

答案 1 :(得分:0)

我对春天无能为力,但这个密码能帮到你想要的吗?

MATCH (d:Decision)<-[:VOTED_FOR]-(v:Vote)-[:VOTED_ON]->(c:Criterion)
WHERE id(d) = {decisionId} 
  AND id(c) = {criterionId} 
WITH v
MATCH v-[:CREATED_BY]->(u:User)
RETURN {id: v.id
       , weight: v.weight
       , `author.id`: u.id
       , `author.username`: u.username
       ... } as v