I have used jackson JsonIdentityInfo to handle the recursive object reference in spring mvc. I came across one issue i.e., Unable to deserialize Json that contain 2 objects with the same ID.
{
"organizations": [
{
"organizationId": 1,
"organizationName": "org1",
"enterprise": {
"enterpriseId": 1,
"enterpriseName": "ent1",
"organizations": null
}
},
{
"organizationId": 2,
"organizationName": "org2",
"enterprise": 1
}
]
}
if you see above, both organizations are mapped with enterprise "1". For the first organization it is whole enterprise object but for organization 2, it is giving ID only. I need to get the whole object for organization 2 as well.
My POJO declarations:
@Entity
@Table(name = "organization")
@JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "organizationId")
public class Organization implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "organization_id")
private Long organizationId;
...
@ManyToOne
@JoinTable(name = "enterprise_organization", joinColumns = {
@JoinColumn(name = "organization_id") }, inverseJoinColumns = { @JoinColumn(name = "enterprise_id") })
private Enterprise enterprise;
...
}
@Entity
@Table(name = "enterprise")
@JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "enterpriseId")
public class Enterprise extends BaseEntity implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "enterprise_id")
private Long enterpriseId;
...
@OneToMany(mappedBy = "enterprise")
private List<Organization> organizations;
...
}
I searched google and SO but no luck.
What are the changes needed to deserialize Json that contain 2 objects with the same ID ?
After a lot of tries, @JsonIgnoreProperties solved my problem.
example: "@JsonIgnoreProperties(allowSetters = true, value = { "enterprise" })"