Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to user @JsonIdentityInfo with ObjectIdGenerators.PropertyGenerator when the IDs from different entity types overlap?

Tags:

java

xml

jackson

Is it possible to user @JsonIdentityInfo with ObjectIdGenerators.PropertyGenerator when the IDs from different entity types overlap?

Assume that I have the following XML and want to deserialize it with Jackson 2.x:

<foo>
  <id>3</id>
  <name>Peter</name>
  <bar>
     <id>3</id>
     <kind>dog</kind>
     <!--belongsTo>3</belongsTo-->
  </bar>
</foo>

I've annotated the Foo and Bar class with @JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "id").

The deserialization fails with com.fasterxml.jackson.databind.JsonMappingException: Already had POJO for id (java.lang.String) [3].

If bar has the id 4, everything works fine.

What are the requirements regarding the IDs in XML to be deserialized by Jackson? I have assumed that - since Jackson knows to which entity scope it would be able to work with the same id as long as it belongs to different types.

P.S.: It doesn't seem to matter the belongsTo reference (pointing to a foo object) is set or not.

like image 985
xwoker Avatar asked Dec 25 '22 14:12

xwoker


1 Answers

You might have found the answer already, but in case you didn't...

You should be able to use the scope parameter of the @JsonIdentityInfo for that, i.e.,

@JsonIdentityInfo(scope=Foo.class, property="id", generator=ObjectIdGenerators.PropertyGenerator.class)
public class Foo { ... }

and

@JsonIdentityInfo(scope=Bar.class, property="id", generator=ObjectIdGenerators.PropertyGenerator.class)
public class Bar { ... }
like image 135
dsg Avatar answered Jan 13 '23 15:01

dsg