在我的春節,啓動/ Neo4j的應用程序,我有以下實體: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
查詢,並沒有獲取從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)
在SDN3你不能只是創建地圖,並將它僞裝成一個NodeEntity,這就是@QueryResult註釋DTO是爲了。看文檔。 – 2015-04-02 18:41:24
我想避免讓用戶作爲投票的成員,同時作爲@QueryResult(投票,用戶)中的獨立實體..我認爲這是多餘的。 – alexanoid 2015-04-02 18:48:50