0

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 ?

Krish
  • 1,804
  • 7
  • 37
  • 65
  • what happens if you change `private List organizations;` to `private Set organizations;` – Patrick Nov 02 '17 at 15:43
  • @Patrick I have changed List to Set. Still same issue. FYI, I am using mapping table. So NO issue with the datatype. – Krish Nov 02 '17 at 15:53
  • @Krishna Dont think you can do that in a simple generic way. Probably can try https://stackoverflow.com/a/24684251/1032167 or implement custom id Generator, but i think it will be much harder to read and understand then custom serializer/deserializer. – varren Nov 02 '17 at 16:34
  • @Krishna btw if you need only deserialization, you can look at https://github.com/varren/JsonIdentityInfoAlwaysAsObjectSerializationJackson had similar problem back in the days. It is more like a hack that a solution... – varren Nov 02 '17 at 16:44
  • @varren thanks for your response. In the link you provided, it is hard coded with "id", how can I do it for different id names like "enterpriseId", "organizationId" etc.. ? – Krish Nov 02 '17 at 19:07

1 Answers1

0

After a lot of tries, @JsonIgnoreProperties solved my problem.

example: "@JsonIgnoreProperties(allowSetters = true, value = { "enterprise" })"

Krish
  • 1,804
  • 7
  • 37
  • 65