JsonIdentityInfo为仅设置数组中某些对象的ID的原因是什么?

huangapple go评论69阅读模式
英文:

Why does JsonIdentityInfo only set the id for some objects in the array?

问题

I need to understand a problem that happens to me when I use the JsonIdentityInfo notation of the Jackson library. I noticed that for some objects in the return list the entire object does not appear but only the id. Can any of you tell me why?
Below is my code and return data.

@Data
@Entity
@Table(name = "attivita", schema = "public")
@AllArgsConstructor @NoArgsConstructor
@JsonIgnoreProperties({"hibernateLazyInitializer", "handler"})
public class Attivita implements Serializable {

  @Id 
  @GeneratedValue(strategy = GenerationType.IDENTITY)
  @Column(name = "id", nullable = false)
  private Long id;

    @JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "id")
    @OneToMany(mappedBy = "attivita", cascade = CascadeType.ALL, fetch = FetchType.LAZY, orphanRemoval = true)
    private List<Lavorazione> listaLavorazioni;
  
}

@Data
@Entity
@Table(name = "lavorazione", schema = "public")
@AllArgsConstructor @NoArgsConstructor
@JsonIgnoreProperties({"hibernateLazyInitializer", "handler"})
public class Lavorazione implements Serializable {
    
    //ATTRIBUTI
    @Id 
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id", nullable = false)
    private Long id;

@JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "id")
	@ManyToOne(targetEntity = Attivita.class, fetch = FetchType.LAZY, optional = false)
	@JoinColumn(name="fk_attivita", nullable = false, updatable = false)
	private Attivita attivita;

}

Below is the response received, where it can be noted that for the first object (id=1) I have the "activity" object inside, while only the id=3 appears in the second.

JsonIdentityInfo为仅设置数组中某些对象的ID的原因是什么?

Thank you all.

英文:

I need to understand a problem that happens to me when I use the JsonIdentityInfo notation of the Jackson library. I noticed that for some objects in the return list the entire object does not appear but only the id. Can any of you tell me why?
Below is my code and return data.

    @Data
@Entity
@Table(name = &quot;attivita&quot;, schema = &quot;public&quot;)
@AllArgsConstructor @NoArgsConstructor
@JsonIgnoreProperties({&quot;hibernateLazyInitializer&quot;, &quot;handler&quot;})
public class Attivita implements Serializable {
@Id 
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = &quot;id&quot;, nullable = false)
private Long id;
@JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = &quot;id&quot;)
@OneToMany(mappedBy = &quot;attivita&quot;, cascade = CascadeType.ALL, fetch = FetchType.LAZY, orphanRemoval = true)
private List&lt;Lavorazione&gt; listaLavorazioni;
}
@Data
@Entity
@Table(name = &quot;lavorazione&quot;, schema = &quot;public&quot;)
@AllArgsConstructor @NoArgsConstructor
@JsonIgnoreProperties({&quot;hibernateLazyInitializer&quot;, &quot;handler&quot;})
public class Lavorazione implements Serializable {
//ATTRIBUTI
@Id 
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = &quot;id&quot;, nullable = false)
private Long id;
@JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = &quot;id&quot;)
@ManyToOne(targetEntity = Attivita.class, fetch = FetchType.LAZY, optional = false)
@JoinColumn(name=&quot;fk_attivita&quot;, nullable = false, updatable = false)
private Attivita attivita;
}

Below is the response received, where it can be noted that for the first object (id=1) I have the "activity" object inside, while only the id=3 appears in the second.

JsonIdentityInfo为仅设置数组中某些对象的ID的原因是什么?

Thank you all.

答案1

得分: 1

我注意到返回列表中的某些对象并不是整个对象都显示出来,只有ID。

关于这种行为的解释包含在JsonIdentityInfo文档中,其中指定了序列化是通过将第一个实例作为完整对象和对象标识进行的,而对于对该对象的其他引用,则作为引用值进行序列化。因此,对于第一个匹配项,对象将被完全序列化,而其他匹配项将被引用为ID。在您的示例中,如果没有采用这种行为,由于AttivitaLavorazione类之间的循环引用,您将会遇到无限递归问题。

英文:

> I noticed that for some objects in the return list the entire object
> does not appear but only the id.

An explanation of this behaviour is contained in the JsonIdentityInfo documentation where it is specified that serialization is done by serializing the first instance as full object and object identity, and other references to the object as reference values, so for the first match the the object will be fully serialized while others matches will be referred as id. In your example if this behaviour has not been adopted you would have had an infinite recursion issue due to the circular reference of Attivita and Lavorazione classes.

huangapple
  • 本文由 发表于 2023年6月26日 18:58:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/76556057.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定