MongoJack support @javax.persistance.Id doesn't work

653 Views Asked by At

I am using MongoJack (2.0.0) to serialize/deserialize objects from MongoDB. According http://mongojack.org/index.html MongoJack should support @javax.persistance.Id.

I have annotated object variable with @Id

@Id
private String id;

When I try to save the object with a valid id

jacksonDBCollection.save(myEntity);

The object is saved as new document instead of updating the existing one.

I do use custom deserializer but no serializer in my module:

ObjectMapper objectMapper = new ObjectMapper();

SimpleModule module = new SimpleModule("MyModule", Version.unknownVersion());
module.addDeserializer(MyEntity.class, new MyEntityJsonDeserializer());
objectMapper.registerModule(module);

return objectMapper;

I have debugged some code and see that the id is treated as String instead of ObjectId when calling jacksonDBCollection.save(myEntity). It looks that support for @javax.persistance.Id doesn't work.

I have tried to find the source where this is supported but no luck. Can anyone point me the source where this is supported and/or let me know what I am doing wrong?

Cheers.

1

There are 1 best solutions below

2
On

When you are configuring your objectMapper you should also invoke:

MongoJackModule.configure(objectMapper);

to register MongoAnnotationIntrospector which detects and interprets the @javax.persistance.Id annotation.