1

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.

Tuno
  • 1,234
  • 1
  • 13
  • 22

1 Answers1

2

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.

Lukasz Wiktor
  • 19,644
  • 5
  • 69
  • 82
  • MongoJackModule is part of the org.mongojack.internal package that is not exposed in OSGi context. I did forget to mention that I use OSGi as it was not relevant for this question. Internal indicates that is shouldn't be called directly. Any idea how to configure this differently? – Tuno Jun 24 '14 at 11:32
  • Briljant! I was grinding my teeth on why `collection.updateById()` wasn't updating any document. This solved the isue. – Bart Aug 10 '14 at 05:34