1、前言

在使用Neo4j-ogm时,对于自定义的NodeEntity和NodeRelation存在循环引用的关系时,在jackson序列化时会导致序列化失败,使用一个注解用来解决循环引用。

2、实体类

  • NodeEntity节点对象
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
@org.neo4j.ogm.annotation.NodeEntity(label = "ENTITY")
public class NodeEntity extends Neo4jAbstractBaseEntity {

// ...省略其他字段

/**
* 指向当前实例的节点
*/
@Relationship(type = "RELATION", direction = Relationship.INCOMING)
private Set<NodeRelation> fromEntity;

/**
* 当前实例指向的节点
*/
@Relationship(type = "RELATION")
private Set<NodeRelation> toEntity;

}
  • NodeRelation关系对象
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
@RelationshipEntity(type = "RELATION")
public class NodeRelation extends Neo4jAbstractBaseEntity {

// ...省略其他字段

/**
* 发出关系的实例
*/
@StartNode
private NodeEntity start;

/**
* 关系终止的实例
*/
@EndNode
private NodeEntity end;

}

3、@JsonIdentityInfo注解

@JsonIdentityInfo 是 Jackson 库中的一个注解,用于处理对象之间的循环引用问题,同时还可以在 JSON 序列化和反序列化过程中保留对象的身份信息。这对于处理复杂对象图(例如,存在相互引用的对象)

3.1 基本使用

  1. 指定注解:您需要在实体类上添加 @JsonIdentityInfo 注解。

  2. 选择生成器:该注解需要一个生成器(generator)来生成对象的唯一标识符。常用的生成器是 ObjectIdGenerators.PropertyGenerator,它允许您指定一个字段作为唯一标识符。

  3. 指定属性:使用 property 属性指定一个字段名,这个字段将在序列化时作为对象的唯一标识符。这个字段不必是实体的一部分,Jackson 会自动处理它。

3.2 使用示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
@JsonIdentityInfo(
generator = ObjectIdGenerators.PropertyGenerator.class,
property = "id")
public class NodeEntity {
private String id;
// 其他字段和关系
}

@JsonIdentityInfo(
generator = ObjectIdGenerators.PropertyGenerator.class,
property = "id")
public class NodeRelation {
private String id;
private NodeEntity fromEntity;
private NodeEntity toEntity;
// 其他字段
}

在这个示例中,id 字段用作每个 NodeEntity 和 NodeRelation 对象的唯一标识符。当 Jackson 序列化这些对象时,它会自动处理它们之间的相互引用,避免了循环引用的问题。

3.3 注意事项

  • 唯一标识符: 确保您用于 property 的字段在所有实例中是唯一的。

  • 对象图的复杂性: 尽管 @JsonIdentityInfo 可以解决循环引用问题,但对于非常复杂的对象图,仍然可能需要其他的处理策略。

  • 一致性: 确保在所有相关的类上一致地应用此注解,以保证整个序列化/反序列化过程的一致性。