Spring Data Neo4j 4和动态产品属性 [英] Spring Data Neo4j 4 and dynamic product properties

查看:453
本文介绍了Spring Data Neo4j 4和动态产品属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Neo4j/SDN项目中,我必须执行以下任务-我有一个父节点(比如说ParentNode),它必须定义一组产品特征,例如:

In my Neo4j/SDN project I have to implement a following task - I have a parent Node(let's say ParentNode) that must define a set of product characteristics, for example:

weight: Double
size: Integer
license: String
active: Boolean

此外,可以在应用程序运行时添加新产品特征(具有任何新名称和类型).

Also, new product characteristics(with any new name and type) can be added during the application runtime.

ParentNode可以有一组子节点(比如说ProductNode),并且每个这些节点都可以为ParentNode定义的特定特征提供自己的值.

ParentNode can have a set of child nodes(let's say ProductNode) and each of these nodes can provide an own value for a certain characteristic defined by a ParentNode.

基于这些特征名称和值,我需要有可能过滤ProductNode节点.

Based on these characteristics names and values I need to have possibility to filter a ProductNode nodes.

以前,此结构由SDN3 DynamicProperties由我实现,但AFAIK-DynamicProperties支持已从SDN 4中删除.

Previously this structure have been implemented by me by SDN3 DynamicProperties but AFAIK - DynamicProperties support has been removed from SDN 4.

所以我的问题是-如何基于SDN 4实现以下结构?

So my question is - how to implement the following structure based on SDN 4 ?

已更新

此外,将每个ParentNode特征定义为一个单独的节点(比如说CharacteristicNode)

Also, how about the idea to define every ParentNode characteristic as a separate node(let's say CharacteristicNode)

例如

CharacteristicNode.name = weight
CharacteristicNode.type = Double

CharacteristicNode.name = license
CharacteristicNode.type = String

...

和每个ProductNode可以提供与ProductNodeCharacteristicNode关联的值节点(CharacteristicValueNode).

and every ProductNode can provide a value node(CharacteristicValueNode) associated with ProductNode and CharacteristicNode.

这里的主要问题是如何支持CharacteristicValueNode的不同类型以及如何根据不同的特征及其值过滤ProductNode节点?

The main question here how to support different types for CharacteristicValueNode and how to filter ProductNode nodes based on different characteristic and their values?

推荐答案

在SDN 4中,您可以将这些属性建模为(属性名称,值)的映射,并编写

In SDN 4, you can model these properties as a Map of (property name, value) and write a custom converter. This will convert the Map to a String property on the node (json style perhaps), and then from the graph back to your Map.

缺点是,为这些动态属性编写自定义查询并不容易,因为它们并未真正作为独立属性存储在图形中,而是您的转换器将它们压缩为一个.

The downside to this is that it is not easy to write a custom query for these dynamic properties because they're not really stored in the graph as independent properties- rather, your converter will squash them into a single one.

更新

如果要将每个Characteristic类型定义为一个节点(在您的示例中,您将有4个节点-一个分别代表体重,尺寸,活动状态,许可证),那么您就不需要中间的关联ProductNodeCharacteristicNode.取而代之的是,您可以根据ProductNodeCharacteristicNode之间的关系对特征的农产品价值建模. 例如,如果ProductNode仅具有权重和大小,则您将具有两个关系-一个从ProductNode到权重CharacteristicNode(具有该关系的权重值),另一个从ProductNode到大小带有该关系的大小值.

If you were to define each Characteristic type as a node (in your example, you would have 4 nodes- one representing each of weight,size,active,license), then you don't need an intermediate CharacteristicValueNode to relate the ProductNode and the CharacteristicNode. Instead, you can model the value of the produce for the characteristic on the relationship between the ProductNode and CharacteristicNode. For example, if the ProductNode had only weight and size, then you would have two relationships- one from ProductNode to the weight CharacteristicNode with the weight value on the relationship, and another from the ProductNode to the size CharacteristicNode with the size value on that relationship.

在SDN 4中,这些将被建模为RelationshipEntities.例如,

In SDN 4, these would be modelled as RelationshipEntities. For example,

@RelationshipEntity(type="CHARACTERISTIC")
public class ProductCharacteristic {

Long id;
@StartNode ProductNode product;
@EndNode CharacteristicNode characteristic;
int value;
...

}

ProductNode将包含这些关系实体的集合

The ProductNode would contain a collection of these relationship entities

Set<ProductCharacteristic> characteristics;

然后,您可以查询与具有特定名称的特征相关的产品.或使用findByCharacteristicName

Then you can query for products related to characteristics with a certain name. Or query for ProductCharacteristic with findByCharacteristicName

我还没有真正尝试过这种方法,但是值得考虑一下这种变化会迫使您的基础图形模型支持动态属性.

I haven't really tried this approach out, but it is worth thinking about the change that this forces in your underlying graph model to support dynamic properties.

这篇关于Spring Data Neo4j 4和动态产品属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆