neo4j java节点动态属性 [英] neo4j java node dynamic properties

查看:525
本文介绍了neo4j java节点动态属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建具有动态属性的特定类型的节点. 例如:我可以创建一个具有name,age,address属性的Person节点.但是,当我创建另一个Person节点时,这些属性不必是唯一的属性.该节点可以具有名称,年龄,地址和其他财产工资.使用spring数据或查询DSL需要我创建具有固定数量的实例变量name,age和address的Java POJO类Person.

I am trying to create nodes of a specific type with properties which can be dynamic . For Example : I can create a Person node with name,age,address properties. But these need not be the only properties when I create another Person node. This node can have name,age,address and an additional property salary. Using spring data or query DSL needs me to create Java POJO class Person with fixed number of instance variables name,age and address .

@NodeEntity
public class Person {
@GraphId private Long id;

private String name;
private String age;
private String address;
}

我无法为另一个Person节点添加工资的动态属性.有什么办法可以做到这一点?

I cannot add a dynamic property for salary for another Person node. Is there a way I can achieve this ?

推荐答案

Neo4j-OGM目前不支持动态属性(请参见

Dynamic properties are not supported in Neo4j-OGM at the moment (see https://jira.spring.io/browse/DATAGRAPH-555)

如果仅通过OGM与图形进行交互,而不必查询单个动态属性,则可以尝试使用带有自定义转换器的属性映射,该转换器将该映射转换为字符串(例如json).然后,OGM将使用此转换器将图与图进行序列化. 请注意,由于这些值被压缩为一个字符串,因此现在查询单个动态属性已经不那么容易了.

If you only interact with your graph via the OGM and do not have to query on individual dynamic properties, you could try a Map of properties with a custom Converter, that converts this Map to a String (like json). The OGM will then use this converter to serialize the map to and from the graph. Note that because the values are squashed into a String, it is now not trivial to query on an individual dynamic property.

要创建自定义转换器,您需要实现org.neo4j.ogm.typeconversion.AttributeConverter并提供将Map转换为String的实现. 然后,像这样在域实体中注释地图属性:

To create a custom converter you need to implement org.neo4j.ogm.typeconversion.AttributeConverter and provide the implementation to convert from a Map to String. Then, annotate your map property in your domain entity like this:

 @Convert(MoneyConverter.class)

正如迈克尔指出的那样,如果薪金是唯一的额外可选属性,那么拥有此属性是有意义的,但只有在它具有值时才进行设置.在这种情况下,动态属性会显得过大.当节点具有一组未知的任意属性时,可能需要使用动态属性

As pointed out by Michael, if the salary is the only extra optional property, then it makes sense to have this property but set it only when it has a value. Dynamic properties are overkill in this case. You may want to use dynamic properties when you have an unknown and arbitrary set of properties to be persisted with the node

这篇关于neo4j java节点动态属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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