Problem Description.
So I have an item that I want to use with MongoDB
@Value.Immutable
@Gson.TypeAdapters
@Criteria.Repository
interface Person {
@Criteria.Id
String id();
String fullName();
}
And in-order to support pojos, I've created a MongoClient with the following settings:
CodecRegistry pojoCodecRegistry = fromProviders(PojoCodecProvider.builder().automatic(true).build());
CodecRegistry codecRegistry = fromRegistries(MongoClientSettings.getDefaultCodecRegistry(), pojoCodecRegistry);
MongoClientSettings clientSettings = MongoClientSettings.builder()
.applyConnectionString(connectionString)
.codecRegistry(codecRegistry)
.build();
However, whenever I try to perform an insert operation, I receive an error whereas bson can't find the codec for the Immutable class.
The code that creates the problem:
MongoCollection<Person> people = db.getCollection("peoples", Person.class).withCodecRegistry(pojoCodecRegistry);
Person person = ImmutablePerson.builder()
.id("1")
.fullName("person")
.build();
InsertOneResult result = people.insertOne(shahar);
And the error:
Exception in thread "main" org.bson.codecs.configuration.CodecConfigurationException: Can't find a codec for class with.immutables.ImmutablePerson.
I've tried registering a ClassModel to CodecRegistry for ImmutablePerson as shown below
.register(ClassModel.builder(ImmutablePerson.class).enableDiscriminator(true).build())
However, it saves the "instance" instead of the data in it
Question
What needs to be changed so that the simple insert operation works? Is it possible to do it with immutables?