英文:
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.
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 = "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.
Thank you all.
答案1
得分: 1
我注意到返回列表中的某些对象并不是整个对象都显示出来,只有ID。
关于这种行为的解释包含在JsonIdentityInfo
文档中,其中指定了序列化是通过将第一个实例作为完整对象和对象标识进行的,而对于对该对象的其他引用,则作为引用值进行序列化。因此,对于第一个匹配项,对象将被完全序列化,而其他匹配项将被引用为ID。在您的示例中,如果没有采用这种行为,由于Attivita
和Lavorazione
类之间的循环引用,您将会遇到无限递归问题。
英文:
> 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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论