[英]'Direct self-reference leading to cycle' Jackson and inheritance
I have a class something like this: 我有一堂这样的课:
@Entity
@Inheritance(strategy = InheritanceType.JOINED)
@Audited
@DiscriminatorColumn(name = "tipo_hito")
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(propOrder = { "idHito" })
@XmlRootElement(name = "Hito")
public abstract class Hito implements Ordenable {
@Id
@GeneratedValue(strategy= GenerationType.IDENTITY)
@Column(name = "idhito", nullable = false)
private Long idHito;
public Long getIdHito() { return idHito; }
public void setIdHito(Long idHito) { this.idHito = idHito; }
}
And a class that extends the class above 还有一个扩展了上述类的类
@MappedSuperclass
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(propOrder = {..., "successors", "predecessors", ...})
@XmlRootElement(name = "HitoOrdenable")
public abstract class HitoOrdenable extends Hito {
@XmlElement(name = "predecessors")
@ManyToMany(fetch = FetchType.LAZY)
@JoinTable(name = "succession", joinColumns = { @JoinColumn(name = "idpredecessor") }, inverseJoinColumns = { @JoinColumn(name = "idsuccessor") })
private Set<Hito> predecessors;
@XmlElement(name = "successors")
@ManyToMany(fetch = FetchType.LAZY)
@JoinTable(name = "succession", joinColumns = { @JoinColumn(name = "idsuccessor") }, inverseJoinColumns = { @JoinColumn(name = "idpredecessor") })
private Set<Hito> successors;
// More code...
}
Now, the error it's when I try to get a HitoOrdenable
. 现在,错误是当我尝试获取
HitoOrdenable
。 Because for example I can have: 因为例如,我可以拥有:
Hito1
Hito2-SuccessorOf1
Hito3-SuccessorOf1
Hito4-SuccessorOf3
Hito5
Hito6-SuccessorOf5
So, when I get Hito1
this has Successors Hito2
and Hito3
. 因此,当我获得
Hito1
时, Hito1
具有Hito2
和Hito3
后继者。 But when I get Hito2
it has predecessor Hito1
. 但是当我得到
Hito2
它的前身是Hito1
。 So start a "infinite cycle". 因此,开始一个“无限循环”。
The exact error is: 确切的错误是:
com.fasterxml.jackson.databind.JsonMappingException: Direct self-reference leading to cycle (through reference chain: ar.gob.buenosaires.esb.domain.message.ProyectoRespMsg["proyectos"]->java.util.ArrayList[5]->ar.gob.buenosaires.domain.Proyecto["obras"]->org.hibernate.collection.internal.PersistentBag[0]->ar.gob.buenosaires.domain.Obra["hitos"]->org.hibernate.collection.internal.PersistentBag[0]->ar.gob.buenosaires.domain.HitoObra["hitoOrdenable"])
com.fasterxml.jackson.databind.JsonMappingException:直接自引用导致循环(通过参考链:ar.gob.buenosaires.esb.domain.message.ProyectoRespMsg [“ proyectos”]-> java.util.ArrayList [5] - > ar.gob.buenosaires.domain.Proyecto [ “奥夫拉斯”] - > org.hibernate.collection.internal.PersistentBag [0] - > ar.gob.buenosaires.domain.Obra [ “hitos”] - >的有机hibernate.collection.internal.PersistentBag [0] - > ar.gob.buenosaires.domain.HitoObra [ “hitoOrdenable”])
I tried to set @JsonIgnore in predecessors
but it doesn't works. 我试图在以前的
predecessors
设置@JsonIgnore,但是它不起作用。
After a few days of trys. 经过几天的尝试。 The only solution than we find was to add in
@JsonIgnoreProperties({"predecessors"})
and @JsonIdentityInfo(generator=ObjectIdGenerators.IntSequenceGenerator.class, property="idHitoAux")
我们发现的唯一解决方案是添加
@JsonIgnoreProperties({"predecessors"})
和@JsonIdentityInfo(generator=ObjectIdGenerators.IntSequenceGenerator.class, property="idHitoAux")
HitoOrdenable.java HitoOrdenable.java
@MappedSuperclass
@XmlType(propOrder = {..., "successors", "predecessors", ...})
@XmlAccessorType(XmlAccessType.FIELD)
@JsonIgnoreProperties({ "predecessors" })
@JsonIdentityInfo(generator=ObjectIdGenerators.IntSequenceGenerator.class, property="idHitoAux")
Hito.java Hito.java
@Entity
@Inheritance(strategy = InheritanceType.JOINED)
@Audited
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(propOrder = { "idHito", "tipoHito"})
@JsonIgnoreProperties({"idHitoAux", "hibernateLazyInitializer", "handler"})
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.EXISTING_PROPERTY, visible = true, property = "tipoHito")
@JsonSubTypes({
@JsonSubTypes.Type(name = "hito_obra", value = HitoObra.class),
@JsonSubTypes.Type(name = "hito_proyecto", value = HitoProyecto.class)
})<
With JsonIgnoreProperties we prevent infinite cycle between sucessors and predecessors and with @JsonIdentityInfo
it was because if we use "idHito"
instead of "idHitoAux"
(who its equals to idHito) Hibernate transformed idHito to null. 使用JsonIgnoreProperties可以防止后继者和前任者之间的无限循环,而使用
@JsonIdentityInfo
则是因为如果使用"idHito"
而不是"idHitoAux"
(其等于idHito),则Hibernate会将idHito转换为null。 So with "idHitoAux"
Hibernate can identify every object in the List. 因此,使用
"idHitoAux"
Hibernate可以识别列表中的每个对象。
PD: After that solution we had a problem with deserialization of abstract class Hito
, so our solution it wass to add @JsonSubTypes
. PD:在该解决方案之后,我们遇到了
abstract class Hito
反序列化问题,因此我们的解决方案是添加@JsonSubTypes
。
Greetings! 问候!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.