Neo4j 一对多获取数据
Neo4j one-to-many fetch data
在我的 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.username
和 Vote.author.id
的查询进行提取,而不从与 Vote
实体关联的 User
实体中提取所有其他信息?不管怎样,现在我只有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.underlyingObjectToObject(QueryResultBuilder.java:86)
我帮不上忙 spring 但是这个密码能满足您的需求吗?
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
尝试使用 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);
在我的 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.username
和 Vote.author.id
的查询进行提取,而不从与 Vote
实体关联的 User
实体中提取所有其他信息?不管怎样,现在我只有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.underlyingObjectToObject(QueryResultBuilder.java:86)
我帮不上忙 spring 但是这个密码能满足您的需求吗?
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
尝试使用 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);