简体   繁体   English

Map Cypher 查询结果到 DTO

[英]Map Cypher query result to DTO

I want to map cypher query results to a DTO/POJO class.我想将 map cypher 查询结果发送到 DTO/POJO class。 I have the following entities defined in neo4j:我在 neo4j 中定义了以下实体:

  1. Products, which has properties;具有属性的产品; Name, Title, Address姓名、职务、地址
  2. Sellers, which has properties;拥有房产的卖家; Name, Id姓名,身份证
  3. Listings, which has properties;具有属性的列表; Name, Id姓名,身份证

Relationshps are defined as: Products -> Sellers & Sellers -> Listings关系定义为:产品 -> 卖家和卖家 -> 列表

My query results is List of Product.Name, [ {Listings.Name, Listings.Id, Sellers.Id, Sellers.Name} ] .我的查询结果是 List of Product.Name, [ {Listings.Name, Listings.Id, Sellers.Id, Sellers.Name} ] I wish to map this to a DTO, I am not able map this result which has different nodes and labels to a DTO/POJO class.我希望将 map 发送到 DTO,我无法 map 这个结果与 DTO/POJO class 具有不同的节点和标签。

As you have already noticed, Spring Data Neo4j is more strict when it comes to map "arbitrary" data that is not directly applicable to one domain entity.正如您已经注意到的那样,Spring 数据 Neo4j 对 map 不直接适用于一个域实体的“任意”数据更为严格。 But on the other hand Spring Data Neo4j also offers support for mapping loose data with the Neo4jClient .但另一方面 Spring 数据 Neo4j 也支持使用Neo4jClient映射松散数据。 Example:例子:

class SoldProductInformation {
   String productName;
   Set<SellingInformation> sellingInformation;
}

class SellingInformation {
   String listingsName;
   String listingsId;
   String sellerName;
   String sellerId
}
neo4jClient.query("...return product.name as productName, someListWithTheInformationFromTheQuestion")
  .fetchAs(SoldProductInformation.class)
  .mappedBy((TypeSystem t, Record record) -> {
         String productName = record.get("productName").asString();
         List<SellingInformation> sellingInformations = record.get("someListWithTheInformationFromTheQuestion").asList(value -> {
             String listingsName = value.get("listingsName").asString();
             // same for listingsId, sellerName, sellerId...
             return new SellingInformation(....);
         });
         return new SoldProductInformation(....);
  })

If you have more entity aligned fields and/or maybe return also nodes, you can make use of the derived mapping function:如果您有更多的实体对齐字段和/或可能还返回节点,则可以使用派生映射 function:

BiFunction<TypeSystem, MapAccessor, Product> mappingFunction = neo4jMappingContext.getRequiredMappingFunctionFor(Product.class);

and apply it via并通过应用它

neo4jClient.query("...return product,...")
  .fetchAs(SoldProductInformation.class)
  .mappedBy((TypeSystem t, Record record) -> {
      Product product = mappingFunction.apply(typeSystem, record.get("product"));
      String productName = product.getName();
// ....

see https://github.com/spring-projects/spring-data-neo4j/issues/2288#issuecomment-861255508 for a complete example.有关完整示例,请参阅https://github.com/spring-projects/spring-data-neo4j/issues/2288#issuecomment-861255508

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM