我有这个查询,使用 cypher-dsl 构建(b/c MATCH
子句是动态的),结果集包含由 @NodeEntity
表示的节点> 带注释的 POJO(以及其他列)。
我的问题是:有没有办法将动态(无注释)查询的结果包装到 @MapResult
(或以 NodeEntities 作为值的常规 Map)中?
以下方法似乎不起作用,因为GraphRepository
的推断类型必须是Node-或RelationshipEntity:
@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(), ...);
}
}
我使用的是 spring-data-neo4j 版本 2.3.0.M1
提前感谢您的帮助
<小时/>更新:
好的,使用 Neo4jTemplate
的 query
和 convert
方法,完成这项工作:
@Inject
public SearchService(Neo4jTemplate template, ...) {
this.template = template;
...
}
public List<QueryResult> search(...) {
List<QueryResult> result = new ArrayList<>();
Execute cyQuery =
start(...)
...
.returns(
"cust",
"val1",
...
);
for (Map<String, Object> res : this.template.query(cyQuery.toString(), ...)) {
Customer cust = this.template.convert((NodeProxy)res.get("cust"), Customer.class);
result.add(new QueryResult()
.setCustomer(cust)
...
);
}
return result;
}
(假设 Customer 现在是一个类,不再是一个接口(interface))
但是,是否有更好的方法呢?
最佳答案
您应该能够使用query(...).to(CustomerQueryResult.class)
。
此外,您还可以使用 CypherDslRepository
来运行查询,并获得 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> {
}
关于java - 使用 CypherDSL 的 MapResult,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18595511/