java - 使用 CypherDSL 的 MapResult

标签 java spring neo4j cypher spring-data-neo4j

我有这个查询,使用 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

提前感谢您的帮助

<小时/>

更新: 好的,使用 Neo4jTemplatequeryconvert 方法,完成这项工作:

@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/

相关文章:

java - java中的网络爬行

java - 如何将 JAXB 注释与 Spring RestTemplate 一起使用?

csv - Neo4j Cypher-使用LOAD CSV创建节点并设置标签

Java - 如何导航到 android studio 中的 map 网址?

java - 如何创造更真实的爆炸

java - 如何从另一个 xml 文件动态更新 xml 文件?

java - Spring Async 方法隐藏异常

java - 使用动态字段名称反序列化嵌套的 JSON 对象

java - Neo4j CSV 导入类型错误

linux - Windows 操作系统上来自 python 的 Neo4j 服务器 API