neo4j 循环引用问题

背景:
使用spring-data-neo4j 进行crud 数据。在进行大批数据入库时,数据入库到一半,突然提示

Neo.TransientError.Network.CommunicationError"; Code: Neo.TransientError.Network.CommunicationError; Description: Can not start an object, expecting field name

本次实体是(已经真实信息进行处理):

@Getter
@Setter
@ToString
@NodeEntity(label = "person")
public class Person implements Comparable<Person> {

    @GraphId
    Long id;
    @Property(name = "name")
    private String name;
    @Property(name = "code")
    private String code;
    @DateLong
    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private Date create;

    @Relationship(type = "standard")
    private Set<Standard> standardList = new HashSet<>();

    @Relationship(type = "line")
    private Set<LineInfo> lineInfo = new HashSet<>();
	.....
}



@Getter
@Setter
@RelationshipEntity(type = "Standard")
public class Standard {

    @GraphId
    private Long id;

    @StartNode
    @JsonBackReference
    private Site startSite;

    @EndNode
    @JsonBackReference
    private Site endSite;

    private String standCode;
}



开始还以为是有脏数据,比如有空的,非法的数据,先对数据进行排查过滤处理,保证入库的数据关键字段都是合法的。重试。发现问题依旧存在。
经过排查发现。每当我创建@RelationshipEntity时,在dubug 查看数据时可以会在将图形序列化为JSON时遇到无限递归。
虽然通过 @JsonBackReference 避免了序列化,但是貌似还是不生效。当少量的数据时,序列化正常。当数据量大时,跑到一半就报Can not start an object, expecting field name。
通过查阅相关的资料,发现使用JSOG尝试呈现图形只会导致不同的,格式错误的JSON响应。
最后看到github 的小demo (https://github.com/cyclomaniac/neo4j-spring-data-rest-cyclic
这个demo 演示使用小测试用例呈现循环数据的问题。

最终是通过 使用@JsonIgnore注释或配对:@JsonBackReference和@JsonManagedReference
解决循环数据的问题。

最后实体是:

@Getter
@Setter
@RelationshipEntity(type = "Standard")
public class Standard {

    @GraphId
    private Long id;

    @StartNode
    @JsonIgnore
    @JsonBackReference
    private Site startSite;

    @EndNode
    @JsonIgnore
    @JsonBackReference
    private Site endSite;

    private String standCode;
}

补充知识:
@JsonManagedReference和@JsonBackReference 序列化时,
@JsonBackReference标注的属性在会被忽略。
@JsonManagedReference标注的属性则会被序列化。
在序列化时,@JsonBackReference的作用相当于@JsonIgnore,此时可以没有@JsonManagedReference。
反序列化时,如果没有@JsonManagedReference,则不会自动注入@JsonBackReference标注的属性(被忽略的父或子);如果有@JsonManagedReference,则会自动注入自动注入@JsonBackReference标注的属性。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Dream_bin

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值