Description
Roman Puchkovskiy opened DATAMONGO-2620 and commented
It is possible to configure MappingMongoConverter
in such a way that 'class' attribute is not saved to MongoDB. For example, the following setup
MappingContextTypeInformationMapper typeInformationMapper = new MappingContextTypeInformationMapper(
mappingContext);
DefaultMongoTypeMapper typeMapper = new DefaultMongoTypeMapper(DefaultMongoTypeMapper.DEFAULT_TYPE_KEY,
singletonList(typeInformationMapper));
MappingMongoConverter converter = new MappingMongoConverter(NoOpDbRefResolver.INSTANCE, mappingContext);
converter.setTypeMapper(typeMapper);
would produce the following behavior: if an entity class is annotated with @TypeAlias
, then the corresponding type alias is saved in class
attribute to MongoDB, otherwise no such attribute is added.
This setup seems legitimate: one could decide that they do not need class
attribute at all if they do not plan to ever use inheritance for some their entity classes.
But this setup makes Criteria.alike(Example)
queries useless: they do not find anything. This happens, because DefaultMongoTypeMapper#writeTypeRestrictions()
does not check whether restrictedMappedTypes
is empty and adds "{'class': {$in: [...]
"}} restriction even in such an (empty) case, which produces "{'class': {$in: []
"}} which matches nothing as there is simply no class
attribute.
This restriction is needed to make sure that only entities of the 'right' type are found. But if we don't have any type alias information, it seems natural to just drop the restriction
Affects: 2.2.9 (Moore SR9), 3.0.3 (Neumann SR3)
Referenced from: pull request #884