I have an error when trying to work with @JsonIdentityInfo jackson annotation. When I try to deserialize the object I get the following exception:
Could not read JSON: Already had POJO for id (java.lang.Integer) [1] (through reference chain: eu.cobiz.web.domain.Site["operators"]->eu.yavix.web.domain.Account["image"]->eu.cobiz.web.domain.Image["@Image"]);nested exception is com.fasterxml.jackson.databind.JsonMappingException: Already had POJO for id (java.lang.Integer) [1] (through reference chain: eu.yavix.web.domain.Site["operators"]->eu.cobiz.web.domain.Account["image"]->eu.cobiz.web.domain.Image["@Image"])
The JSON I am trying to deserialize looks like:
{ "@Site": 1, "siteId": 1, "name": "0", "address": { "@Address": 2, "addressId": 4, "number": "22" }, "operators": [ { "accountId": 1, "email": "[email protected]", "image": { "@Image": 1, "imageId": 1, "uri": "http://icons.iconarchive.com/icons/deleket/purple-monsters/128/Alien-awake-icon.png" } }, { "accountId": 2, "email": "[email protected]", "image": { "@Image": 2, "imageId": 2, "uri": "http://icons.iconarchive.com/icons/deleket/purple-monsters/128/Alien-awake-icon.png" } } ] }
My domain object is annotated with
@JsonIdentityInfo(generator = ObjectIdGenerators.IntSequenceGenerator.class, property = "@Image")
The problem arises on @Id annotation since if I remove the annotation the problem disappears (as I did for account) but on my understanding the new feature is useful for cyclic dependencies which is useful for me in other scenarios. There shouldn't be a conflict between the 2 images since they are different objects.
How can I solve this or what is the problem?
You should use scope
parameter when annotating the ids. Then the de-serializer would make sure the id is unique within the scope.
From Annotation Type JsonIdentityInfo
:
Scope is used to define applicability of an Object Id: all ids must be unique within their scope; where scope is defined as combination of this value and generator type.
e.g.
@JsonIdentityInfo(generator=ObjectIdGenerators.IntSequenceGenerator.class,property="@id", scope = Account.class)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With