I upgraded to Spring Boot 2.2.0.RELEASE and wanted to replace the now deprecated AbstractMongoConfiguration with AbstractMongoClientConfiguration. I am using
codecRegistries.add(CodecRegistries.fromCodecs(new UuidCodec(UuidRepresentation.STANDARD)));
to set the UUID Codec in the MongoDB to STANDARD (UUID) instead of Legacy Codec (LUUID). When looking into the database the Codec stays in the legacy format. Anybody else experienced the same problem?
Old implementation (working):
@Override
public MongoClient mongoClient() {
CodecRegistry codecRegistry =
CodecRegistries.fromRegistries(CodecRegistries.fromCodecs(new UuidCodec(UuidRepresentation.STANDARD)),
MongoClient.getDefaultCodecRegistry());
return new MongoClient(new ServerAddress(address, port), MongoClientOptions.builder().codecRegistry(codecRegistry).build());
}
New implementation (not working):
@Override
public MongoClient mongoClient() {
List<CodecRegistry> codecRegistries = new ArrayList<>();
codecRegistries.add(CodecRegistries.fromCodecs(new DocumentCodec()));
codecRegistries.add(CodecRegistries.fromCodecs(new UuidCodec(UuidRepresentation.STANDARD)));
CodecRegistry codecRegistry = CodecRegistries.fromRegistries(codecRegistries);
return MongoClients.create(MongoClientSettings.builder()
.codecRegistry(codecRegistry)
.applyConnectionString(new ConnectionString(connectionString))
.build());
}
I expected the UUID Codec in the database to adjust to Standard Codec but it stays in Legacy Codec.
@Document is an annotation provided by Spring data project. It is used to identify a domain object, which is persisted to MongoDB. So you can use it to map a Java class into a collection inside MongoDB. If you don't use Spring Data, you don't need this annotation.
Yes, DataNucleus JPA allows it, as well as to many other databases.
Annotation @Document The annotations @Document applied to a class marks this class as a candidate for mapping to the database. The most relevant parameter is value to specify the collection name in the database. The annotation @Document specifies the collection type to DOCUMENT .
MongoRepository extends the PagingAndSortingRepository and QueryByExampleExecutor interfaces that further extend the CrudRepository interface. MongoRepository provides all the necessary methods which help to create a CRUD application and it also supports the custom derived query methods.
I found a solution for the problem. The new UuidCodec(UuidRepresentation.STANDARD)
needs to be at the first position. My Code looks like following:
private static final CodecRegistry CODEC_REGISTRY = CodecRegistries.fromProviders(
Arrays.asList(new UuidCodecProvider(UuidRepresentation.STANDARD),
new ValueCodecProvider(),
new BsonValueCodecProvider(),
new DBRefCodecProvider(),
new DBObjectCodecProvider(),
new DocumentCodecProvider(new DocumentToDBRefTransformer()),
new IterableCodecProvider(new DocumentToDBRefTransformer()),
new MapCodecProvider(new DocumentToDBRefTransformer()),
new GeoJsonCodecProvider(),
new GridFSFileCodecProvider(),
new Jsr310CodecProvider(),
new BsonCodecProvider()));
That behavior is not very nice and it is possibly a bug. Hope this helps some of you.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With