正如您已经注意到的,Spring Data Neo4j 在映射不直接适用于一个域实体的“任意”数据时更加严格。
但另一方面,Spring Data Neo4j 也支持使用Neo4jClient 映射松散数据。
示例:
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(....);
})
如果您有更多的实体对齐字段和/或可能还返回节点,您可以使用派生映射函数:
BiFunction<TypeSystem, MapAccessor, Product> mappingFunction = neo4jMappingContext.getRequiredMappingFunctionFor(Product.class);
并通过
应用它
neo4jClient.query("...return product,...")
.fetchAs(SoldProductInformation.class)
.mappedBy((TypeSystem t, Record record) -> {
Product product = mappingFunction.apply(typeSystem, record.get("product"));
String productName = product.getName();
// ....
有关完整示例,请参阅 https://github.com/spring-projects/spring-data-neo4j/issues/2288#issuecomment-861255508。