vznet/mongo-jackson-mapper — Issue #9: Unable to perform DB updates using Builder when an ObjectMapper is provided
Author: almeidap
Created: 2012-03-05 08:40:25+00:00
State: open

When providing an `ObjectMapper` to `JacksonDBCollection`, database updates through Builder queries are not performed.

Here is a simple test case:

public class BuilderTest {

public static void main(String[] args) throws UnknownHostException, MongoException {
BuilderTest tests = new BuilderTest();
tests.testDBQuery();
}

private final Mongo mongo;
private final DB db;
private DBCollection propertyCollection;

private ObjectMapper mapper;

private JacksonDBCollection jacksonCollection;

public BuilderTest() throws UnknownHostException, MongoException {
mongo = new Mongo("127.0.0.1", 27017);
db = mongo.getDB("tests");
propertyCollection = db.createCollection("Property", null);

mapper = new ObjectMapper();

// Remove "mapper" parameter from 'wrap' method to make it work:
jacksonCollection = JacksonDBCollection.wrap(propertyCollection, Property.class, String.class, mapper);
}

private void testDBQuery() {
Property property = new Property();
property.setKey("foo");

Property saved = jacksonCollection.save(property).getSavedObject();
System.out.println("Saved ID: " + saved.getId());

jacksonCollection.updateById(saved.getId(), DBUpdate.set("value", "bar"));

Property updated = jacksonCollection.findOneById(saved.getId());
System.out.println("Value: " + updated.getValue()); // Throws NullPointerException if an ObjectMapper is provided
}
}

And the `Property` POJO:

public class Property {
private String id;

private String key;
private String value;

@ObjectId
@JsonProperty("_id")
public String getId() {
return id;
}

@ObjectId
@JsonProperty("_id")
public void setId(String id) {
this.id = id;
}

/* Other getters/setters omitted */
}

Maven dependencies:

org.mongodb
mongo-java-driver
2.7.3

net.vz.mongodb.jackson
mongo-jackson-mapper
1.4.0

org.codehaus.jackson
jackson-mapper-asl
1.9.4