Details
-
Improvement
-
Resolution: Unresolved
-
Minor - P4
-
None
-
None
-
None
Description
A user reached out to me because his piece of code wasn't working as expected.
https://gist.github.com/pavangayakwad/5adbece8e11b26508a1338dc032c60ae
He had a POJO with a few fields and 2 of them were named "xAxisColumn" and "yAxisColumn".
All the fields were mapped correctly - he could write them to MDB but not retrieve these 2.
His IDE and mine (IntelliJ) generated the getter and setter like so:
getxAxisColumn and setxAxisColumn (note the lower case X).
And we are expecting getXAxisColumn (upper case X) apparently which respect the CamelCase - but apparently this is not the correct answer as it's not respecting the JavaBeans API specification from 1997
This isn't a big deal per say - and there is an easy workaround. But this is really confusing and not an easy bug to find / understand as it's based on introspection if I understand this correctly.
Here is a piece of code to illustrate better what I'm saying.
import com.mongodb.ConnectionString; |
import com.mongodb.MongoClientSettings; |
import com.mongodb.client.MongoClient; |
import com.mongodb.client.MongoClients; |
import com.mongodb.client.MongoCollection; |
import com.mongodb.client.MongoDatabase; |
import org.bson.codecs.configuration.CodecRegistry; |
import org.bson.codecs.pojo.PojoCodecProvider; |
|
|
import static org.bson.codecs.configuration.CodecRegistries.fromProviders; |
import static org.bson.codecs.configuration.CodecRegistries.fromRegistries; |
|
|
public class MappingPOJOFailed { |
|
|
public static void main(String[] args) { |
ConnectionString connectionString = new ConnectionString("mongodb://localhost"); |
CodecRegistry pojoCodecRegistry = fromProviders(PojoCodecProvider.builder().register(Good.class, Bad.class).build()); |
CodecRegistry codecRegistry = fromRegistries(MongoClientSettings.getDefaultCodecRegistry(), pojoCodecRegistry);
|
MongoClientSettings clientSettings = MongoClientSettings.builder()
|
.applyConnectionString(connectionString)
|
.codecRegistry(codecRegistry)
|
.build();
|
try (MongoClient mongoClient = MongoClients.create(clientSettings)) { |
MongoDatabase db = mongoClient.getDatabase("test"); |
MongoCollection<Good> goodColl = db.getCollection("good", Good.class); |
MongoCollection<Bad> badcoll = db.getCollection("bad", Bad.class); |
|
|
goodColl.insertOne(new Good().setXAxis("I'm here!")); |
System.out.println(goodColl.find().first().getXAxis()); // This line prints "I'm here!" |
|
|
badcoll.insertOne(new Bad().setxAxis("I'm NOT here!")); |
System.out.println(badcoll.find().first().getxAxis()); // This line SHOULD print "I'm NOT here!" but I get "null". |
goodColl.drop();
|
badcoll.drop();
|
}
|
}
|
|
|
public static class Good { |
private String xAxis; |
|
|
public String getXAxis() { |
return xAxis; |
}
|
|
|
public Good setXAxis(String xAxis) { |
this.xAxis = xAxis; |
return this; |
}
|
}
|
|
|
public static class Bad { |
private String xAxis; |
|
|
public String getxAxis() { |
return xAxis; |
}
|
|
|
public Bad setxAxis(String xAxis) { |
this.xAxis = xAxis; |
return this; |
}
|
}
|
}
|
Ouput:
I'm here!
|
null |