java - 如何杰克逊序列化/反序列化物料清单实体
问题描述
我的数据库有许多物料清单结构,包括定义各方之间关系的结构:
第一个互动者倾向于扮演高级角色——例如雇主——而第二个互动者倾向于扮演次要角色——例如雇员。
我希望能够将其序列化为 JSON,但我遇到了问题。
我的实体注释如下:
聚会
@OneToMany(
targetEntity = PartyRelationshipDO.class,
fetch = FetchType.LAZY, cascade = CascadeType.ALL
)
@JoinColumn(name = "first_party_id", referencedColumnName = "party_id")
@JsonProperty("firstParties")
@JsonManagedReference(value = "FirstParty")
private Set<PartyRelationshipDO> firstParties;
@OneToMany(
targetEntity = PartyRelationshipDO.class,
fetch = FetchType.LAZY, cascade = CascadeType.ALL
)
@JoinColumn(name = "second_party_id", referencedColumnName = "party_id")
@JsonProperty("secondParties")
@JsonManagedReference(value = "SecondParty")
private Set<PartyRelationshipDO> secondParties;
当事人关系
@ManyToOne(optional = false)
@JoinColumn(name = "first_party_id", referencedColumnName = "party_id")
@JsonProperty("firstParty")
@JsonBackReference(value = "FirstParty")
private PartyDO firstParty;
@ManyToOne(optional = false)
@JoinColumn(name = "second_party_id", referencedColumnName = "party_id")
@JsonProperty("secondParty")
@JsonBackReference(value = "SecondParty")
private PartyDO secondParty;
这在一定程度上起作用。当我序列化我的测试用例时,我得到:
{
"partyId": 1,
"partyIdentifier": "first.interactor@email.address",
"isDeleted": false,
"partyIdentifierType": {
"typeKey": "EMAIL_ADDRESS"
},
"secondParties": [
{
"effectivePeriodFrom": "2021-11-01T15:55:38.518Z",
"isDeleted": false,
"firstPartyId": 1,
"secondPartyId": 2
}
],
"firstName": "First",
"lastName": "Interactor"
}
问题是,secondParty
对象没有在"secondParties"
数组中序列化。我想看到的是:
{
"partyId": 1,
"partyIdentifier": "first.interactor@email.address",
"isDeleted": false,
"partyIdentifierType": {
"typeKey": "EMAIL_ADDRESS"
},
"secondParties": [
{
"effectivePeriodFrom": "2021-11-01T15:55:38.518Z",
"isDeleted": false,
"firstPartyId": 1,
"secondPartyId": 2,
"secondParty": {
"partyId": 2,
"partyIdentifier": "second.interactor@email.address",
"isDeleted": false,
"partyIdentifierType": {
"typeKey": "EMAIL_ADDRESS"
},
"firstName": "Second",
"lastName": "Interactor"
}
}
],
"firstName": "First",
"lastName": "Interactor"
}
如果我不使用@JsonManagedReference
and @JsonBackReference
,我会得到一个StackOverflowError
重复的堆栈跟踪块:
at com.fasterxml.jackson.databind.ser.std.CollectionSerializer.serialize(CollectionSerializer.java:25)
at com.fasterxml.jackson.datatype.hibernate5.PersistentCollectionSerializer.serialize(PersistentCollectionSerializer.java:243)
at com.fasterxml.jackson.databind.ser.BeanPropertyWriter.serializeAsField(BeanPropertyWriter.java:727)
at com.fasterxml.jackson.databind.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:719)
at com.fasterxml.jackson.databind.ser.BeanSerializer.serialize(BeanSerializer.java:155)
at com.fasterxml.jackson.databind.ser.BeanPropertyWriter.serializeAsField(BeanPropertyWriter.java:727)
at com.fasterxml.jackson.databind.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:719)
at com.fasterxml.jackson.databind.ser.BeanSerializer.serialize(BeanSerializer.java:155)
at com.fasterxml.jackson.databind.ser.std.CollectionSerializer.serializeContents(CollectionSerializer.java:145)
at com.fasterxml.jackson.databind.ser.std.CollectionSerializer.serialize(CollectionSerializer.java:107)
我知道物料清单结构本质上是递归的,但我只对上/下一层感兴趣。所以,作为1st Interactor,列出我所有的2nd Interactors,或者作为2nd Interactor,列出我所有的1st Interactors。
这是如何实现的?