Neo4j一对多获取数据
在我的Spring Boot/Neo4j应用程序中,我具有以下实体:
In my Spring Boot/Neo4j application I have a following entities:
@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存储库方法:
and I have a following SDN repository method:
@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
一起显示这些投票的清单.
On the client I'd like to display list of these votes with not only user(author) Id
but and with author username
also.
如何基于Vote.author.username
和Vote.author.id
上的查询来获取,并且不从与Vote
实体相关联的User
实体中获取所有其他信息?无论如何,现在我只有Vote.author.id
值.
How to fetch based on the query above Vote.author.username
and Vote.author.id
and do not fetch all other information from User
entity associated with Vote
entity? Anyway, right now I only have Vote.author.id
value.
已编辑
以下查询
@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);
不适用于以下例外情况:
doesn't work with a following exception:
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)
尝试使用DTO(也可以是与getter的接口):
Try this with a DTO (can also be an interface with getters):
@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);