spring-data-mongodb icon indicating copy to clipboard operation
spring-data-mongodb copied to clipboard

Enhance reference documentation and cover MongoManagedTypes

Open emoen opened this issue 2 years ago • 9 comments

MongoManagedTypes are not mentioned in the v 4.0 release even though they where released in that version. They are needed when overriding mongoMappingContext(MongoCustomConversions customConversions, MongoManagedTypes types) which are needed for custom converters. Can you update java-doc and give an example where there is 2 or more customConverters?

emoen avatar Apr 14 '23 08:04 emoen

Thanks for pointing out the blank spot in the reference documentation.

As per the Javadoc of the ManagedTypes interface those are:

Types managed by a Spring Data implementation. Used to predefine a set of known entities that might need processing during the initialization phase.

Basically its the initial entity set now exposed as a dedicated type which helps detect them during a potential AOT phase to run inspections required when building a GraalVM native-image.

christophstrobl avatar Apr 14 '23 08:04 christophstrobl

Yes, I found the commit where the change was done: java/org/springframework/data/mongodb/config/MongoConfigurationSupport.java

emoen avatar Apr 14 '23 09:04 emoen

Before it was mappingContext.setInitialEntitySet(getInitialEntitySet()); and now it is mappingContext.setManagedTypes(managedTypes); So my question is - how to I migrate with this change in mind - now that mongoMappingContext() takes a new argument as input? Related stackexchange question https://stackoverflow.com/questions/75996137/what-are-mongomanagedtypes-new-in-spring-data-mongodb-version-4-0

emoen avatar Apr 14 '23 09:04 emoen

Basically its the initial entity set now exposed as a dedicated type which helps detect them during a potential AOT phase to run inspections required when building a GraalVM native-image.

How can I set them?

emoen avatar Apr 14 '23 10:04 emoen

Can you share a complete minimal sample (something that we can unzip or git clone, build, and deploy) that shows what you're trying to achieve. I seem to lack some context form the linked SO question and the error posted there.

christophstrobl avatar Apr 14 '23 16:04 christophstrobl

The attached image shows the problem - in spring-data-mongodb 4.0 the method getMappingBasePackages() which is defined with @ Override is not called anymore: https://ibb.co/ZdChJbQ The stacktrace to the left is using spring-data-mongodb 3.2 - and the stacktrace contains SecurityMongoConfig before the CGLIB spring magic, while in spring-data-mongodb 4.0- there is some CGLIB magic happening before parent classes. Is that causing overriding not to work?

Edit: The error is that after refactoring to create MonoManagedTypes in the mongoMappingContext() - our getMappingBasePages() is not called anymore.

emoen avatar Apr 17 '23 09:04 emoen

Why do you think getMappingBasePackages is no longer called? MongoConfigurationSupport uses getInitialEntitySet to create the MongoManagedTypes bean out of the configured base packages. As already said, please provide a complete minimal sample that reproduces the problem. Screenshots unfortunately contain way to little information to be of value.

christophstrobl avatar Apr 18 '23 15:04 christophstrobl

I know getMappingBasePackages is no longer called! I see it when debugging. The default implementation of getMappingBasePackage is called instead. And that is the reason for the error. A minimal sample would just be any implementation that overrides getMappingBasePackage.

public class FooBarMongoConfig extends AbstractFooBarMongoConfig { .... @Override protected Collection<String> getMappingBasePackages() { return List.of("com.foo.bar.entity"); } }

public abstract class AbstractFooBarMongoConfig extends AbstractReactiveMongoConfiguration { ... }

emoen avatar Apr 18 '23 18:04 emoen

Then providing the sample should not be an issue.

christophstrobl avatar Apr 19 '23 05:04 christophstrobl