问题
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
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
.
Based on these characteristics names and values I need to have possibility to filter a ProductNode
nodes.
Previously this structure have been implemented by me by SDN3 DynamicProperties but AFAIK - DynamicProperties support has been removed from SDN 4.
So my question is - how to implement the following structure based on SDN 4 ?
UPDATED
Also, how about the idea to define every ParentNode
characteristic as a separate node(let's say CharacteristicNode
)
for example
CharacteristicNode.name = weight
CharacteristicNode.type = Double
CharacteristicNode.name = license
CharacteristicNode.type = String
...
and every ProductNode
can provide a value node(CharacteristicValueNode
) associated with ProductNode
and CharacteristicNode
.
The main question here how to support different types for CharacteristicValueNode
and how to filter ProductNode
nodes based on different characteristic and their values?
回答1:
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.
Update
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.
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;
...
}
The ProductNode would contain a collection of these relationship entities
Set<ProductCharacteristic> characteristics;
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.
来源:https://stackoverflow.com/questions/38639178/spring-data-neo4j-4-and-dynamic-product-properties