Java 使用cypl映射结果
我使用cypher dsl构建了这个查询(b/cJava 使用cypl映射结果,java,spring,neo4j,cypher,spring-data-neo4j,Java,Spring,Neo4j,Cypher,Spring Data Neo4j,我使用cypher dsl构建了这个查询(b/cMATCH-子句是动态的),结果集包含由@NodeEntity注释的POJO(以及其他列)表示的节点 我的问题是:有没有一种方法可以将动态(非注释)查询的结果包装到@mapreult(或者将NodeEntities作为值的常规映射)中 以下方法似乎不起作用,因为graphRespository的推断类型必须是节点实体或关系实体: @NodeEntity public class Customer { @GraphId Long id
MATCH
-子句是动态的),结果集包含由@NodeEntity
注释的POJO(以及其他列)表示的节点
我的问题是:有没有一种方法可以将动态(非注释)查询的结果包装到@mapreult
(或者将NodeEntities作为值的常规映射)中
以下方法似乎不起作用,因为graphRespository
的推断类型必须是节点实体或关系实体:
@NodeEntity
public class Customer {
@GraphId
Long id;
...
}
@MapResult
public interface CustomerQueryResult {
@ResultColumn("cust")
Customer getCustomer();
@ResultColumn("val1")
int getVal1();
...
}
public interface CustomerQueryRepository extends GraphRepository<CustomerQueryResult> {
}
@Service
public class SearchService {
private CustomerQueryRepository repo;
...
@Inject
public SearchService(CustomerQueryRepository repo, ...) {
this.repo = repo;
...
}
public Iterable<CustomerQueryResult> search(...) {
Execute cyQuery =
start(...)
...
.returns(
"cust",
"val1",
...
);
return this.repo.query(cyQuery.toString(), ...);
}
}
(假设Customer现在是一个类,不再是接口)
但是,有没有更好的方法呢?您应该能够使用
查询(…).to(CustomerQueryResult.class)
还有一个cyprushlrepository
,您可以使用它来运行查询,并获得一个EndResult
,您可以使用它来打开(CustomerQueryResult.class)
public interface CypherDslRepository<T> {
Page<T> query(Execute query, Map<String, Object> params, Pageable page);
Page<T> query(Execute query, Execute countQuery, Map<String, Object> params, Pageable page);
EndResult<T> query(Execute query, Map<String, Object> params);
}
public interface CustomerQueryRepository extends GraphRepository<Customer>,
CypherDslRepository<Customer> {
}
公共接口存储库{
页面查询(执行查询、地图参数、可分页页面);
页面查询(执行查询、执行计数查询、地图参数、可分页页面);
EndResult查询(执行查询,映射参数);
}
公共接口CustomerQueryRepository扩展了GraphRespository,
细胞色素沉着剂{
}
如果您的“更新”实际上是回答问题的内容,您可以将其作为答案发布。鼓励自答问题。this.template.query(cyQuery.toString(),…).to(CustomerQueryResult.class)
效果很好,谢谢。cypstrokerrepository
方法看起来很诱人,但是我在调用CustomerQueryRepository
上的query
时,在调用result.to(CustomerQueryResult.class)之前,已经收到一个运行时异常“无法从包含多个元素的Iterable中提取单个值”
@michael hunger您的意思是:templateRepository.query(query.toString(),map).to(CustomerQueryResult.class)代码>还有Map
作为第二个参数的用途和作用?另外,我不认为cyprostlrepository
将允许我们获取列别名或将结果(如root.id)映射为someValue
类型的数据。。。我尝试过,但它不能总是返回您页面
或最终结果
public interface CypherDslRepository<T> {
Page<T> query(Execute query, Map<String, Object> params, Pageable page);
Page<T> query(Execute query, Execute countQuery, Map<String, Object> params, Pageable page);
EndResult<T> query(Execute query, Map<String, Object> params);
}
public interface CustomerQueryRepository extends GraphRepository<Customer>,
CypherDslRepository<Customer> {
}