我在neo4j数据库中有一个名为“User and Review”的表。必须从spring数据neo4j中获取这些数据。在这里,我不想得到一个模态。但我需要一个特定的价值观。它们是Id,消息,评级等。问题是如何映射来自Db的那些自定义返回值。请帮帮我。
示例查询是:
@Query("MATCH (from:User)-[r:REVIEWED]->(to:User) where r.eventId={0} return ID(r),r.message,r.rating,r.eventId,r.zimId,from.firstName,to.firstName")
public List<ReviewRelatioshipDto> getReviewsByEventIdAndId(Long eventId);
例外:
SEVERE: Servlet.service() for servlet [dispatcher] in context with path [/social] threw exception [Request processing failed; nested exception is java.lang.RuntimeException: Scalar response queries must only return one column. Make sure your cypher query only returns one item.] with root cause
java.lang.RuntimeException: Scalar response queries must only return one column. Make sure your cypher query only returns one item.
答案 0 :(得分:3)
有两个选项 - 一个是使用@QueryResult
因此,ReviewRelationshipDto
必须注释并遵循JavaBean getter约定或使用@Property
。
另一个选项是只返回一个列和值Iterable<Map<String,Object>>
的Map,其中键是列别名,值是列值。
答案 1 :(得分:0)
Luanne提到的@QueryResult
可能是要走的路。
但是,如果你想要一个替代模式来处理视图模型的持久性转换,你可以做的仍然是使用spring数据neo4j填充你的域模型(而不是你的DTO),因为你会正常(在这种情况下它会是一种叫做ReviewRelationship
)的东西。 Spring Data Neo4j不会在此层中映射任何DTO。
然后,您可以在DTO中选择所需的字段。您可以通过将域对象提供给DTO构建器来自己执行此操作,也可以使用现成的内容(如Model Mapper),然后将其传递给要呈现的视图。