欢迎光临
我们一直在努力

Java实现neo4j图谱数据保存出错解决办法

背景

Java实现neo4j图谱保存数据出错:

org.neo4j.driver.v1.exceptions.ClientException: Property values can only be of primitive types or arrays thereof
    at org.neo4j.driver.internal.util.ErrorUtil.newNeo4jError(ErrorUtil.java:62)
    ...

原因

原因:neo4j只支持属性为基本类型或数组(元素只能为基本类型)。

具体的代码如下:

/**
 * 图谱节点:动态增加属性
 */
@NodeEntity
public class GraphNode {
    @Id
    @GeneratedValue(strategy = Neo4JCustomIdStrategy.class)
    private String id;

    private String label;

    private String labelName;

    private String typeUri;

    private Double x;

    private Double y;

    private boolean active;

    @Properties
    private Map<String, Object> icon = new HashMap<>();

    @Properties
    private Map<String, Object> properties = new HashMap<>();

    @JsonIgnoreProperties({ "startNode", "endNode" })
    @Relationship(type = "OWNS")
    private List<Edge> edges = new ArrayList<>();

    ...Omit getter/setter method...

}

前台传来的json数据:

{
      "color": "#7076ed",
      "typeUri": "object.enterprise",
      "size": 1,
      "icon": {
        "color": "#ebf1fc",
        "scale": 1.5,
        "contentStr": "F0F7",
        "content": "",
        "font": "FontAwesome"
      },
      "x": -3.01189349204656,
      "active": false,
      "y": 0.18748324093041402,
      "label": "某公立医院",
      "id": "16560",
      "labelName": "单位",
      "properties": [
        {
          "propertyKey": "entName",
          "displayType": "C",
          "propertyName": "企业(机构)名称",
          "valueType": "string",
          "propertyValue": [
            "某公立医院"
          ],
          "detail": true,
          "queryType": "",
          "statistics": false
        },
        {
          "propertyKey": "uniscId",
          "displayType": "C",
          "propertyName": "统一社会信用代码",
          "valueType": "string",
          "propertyValue": [
            "12370000495571840L"
          ],
          "detail": true,
          "queryType": "",
          "statistics": false
        },
        {
          "propertyKey": "dataSourcesId",
          "displayType": "C",
          "propertyName": "数据来源ID",
          "valueType": "string",
          "propertyValue": [
            "4112"
          ],
          "detail": false,
          "queryType": "",
          "statistics": false
        },
        {
          "propertyKey": "dataSources",
          "displayType": "C",
          "propertyName": "数据来源",
          "valueType": "string",
          "propertyValue": [
            "验证"
          ],
          "detail": true,
          "queryType": "",
          "statistics": false
        },
        {
          "propertyKey": "updateTime",
          "displayType": "C",
          "propertyName": "最后更新时间",
          "valueType": "string",
          "propertyValue": [
            "2019-01-07 19:21:37"
          ],
          "detail": true,
          "queryType": "",
          "statistics": false
        }
      ]
    }

可以看到json数据中properties保存时neo4j并不支持,这里提供一种解决思路:将实体类代码的properties属性改为String类型,以String类型保存,如果查询后返回到前台数据需要json,再使用fastjson等将String转换为json类型。

解决办法

更改实体类代码:

/**
 * 图谱节点:动态增加属性
 */
@NodeEntity
public class GraphNode {
    @Id
    @GeneratedValue(strategy = Neo4JCustomIdStrategy.class)
    private String id;

    private String label;

    private String labelName;

    private String typeUri;

    private Double x;

    private Double y;

    private boolean active;

    @Properties
    private Map<String, Object> icon = new HashMap<>();

    private String properties;

    @JsonIgnoreProperties({ "startNode", "endNode" })
    @Relationship(type = "OWNS")
    private List<Edge> edges = new ArrayList<>();

    ...Omit getter/setter method...

}

返回前台时转换代码:

JSON.parse(graphNode.getProperties());
赞(0)
未经允许不得转载:Ddmit » Java实现neo4j图谱数据保存出错解决办法

评论 抢沙发

登录

找回密码

注册