24

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": "user982701361@yavix.eu",
        "image": {
            "@Image": 1,
            "imageId": 1,
            "uri": "http://icons.iconarchive.com/icons/deleket/purple-monsters/128/Alien-awake-icon.png"
        }
    },
    {
        "accountId": 2,
        "email": "user174967957@yavix.eu",
        "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?

Jason Aller
  • 3,541
  • 28
  • 38
  • 38
Loki
  • 659
  • 1
  • 8
  • 18

2 Answers2

33

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)
Koray Tugay
  • 22,894
  • 45
  • 188
  • 319
Ali
  • 899
  • 2
  • 13
  • 33
7

To avoid id conflict try to use ObjectIdGenerators.PropertyGenerator.class or ObjectIdGenerators.UUIDGenerator.class instead of ObjectIdGenerators.IntSequenceGenerator.class

pdorgambide
  • 1,787
  • 19
  • 33