spring-data-mongodb
spring-data-mongodb copied to clipboard
MongoDB @Id not working for String identifier when upgrading from Spring boot 2.3 to Spring boot 2.6
Hi,
My Model class has following annotaion
@Document(collection="abc") class ABC{ @id private String myId; }
public interface ABCRepo extends MongoRepository<ABC, String>{
}
when I call abcRepo.save(abcObject);
then _id is stored blank in DB when I change to private ObjectId myId;
then save is creating _id but the problem is because of this change I have to change at many place in the source code.
I am using using dependency
MongoDatabaseFactory factory = new SimpleMongoClientDatabaseFactory(springMongoConfig.mongoClient(), springMongoConfig.getDatabaseName()); // Learned from web, prevents Spring from including the _class attribute final MappingMongoConverter converter = new MappingMongoConverter(new DefaultDbRefResolver(factory), mongoMappingContext); converter.setTypeMapper(new DefaultMongoTypeMapper(null)); converter.afterPropertiesSet();//This step is required to use default converter
any idea what could be the issue ?
I had to also use @JsonSerialize(using= ToStringSerializer.class) @id private ObjectId myId;
because above field is serialized and the serialization was not converting to hex String.
I am assuming below is the culprit for String myId not working, but not very sure, if I can get any help that will be great!
final MongoMappingContext mongoMappingContext = new MongoMappingContext(); mongoMappingContext.setApplicationContext(appContext);//So that SPEL can be used here for ex.@Document(collection = "#{@environment.getProperty('app.event.collection_name')}") MongoDatabaseFactory factory = new SimpleMongoClientDatabaseFactory(springMongoConfig.mongoClient(), springMongoConfig.getDatabaseName()); // Learned from web, prevents Spring from including the _class attribute final MappingMongoConverter converter = new MappingMongoConverter(new DefaultDbRefResolver(factory), mongoMappingContext); converter.setTypeMapper(new DefaultMongoTypeMapper(null)); converter.afterPropertiesSet();//This step is required to use default converter MongoTemplate template =new MongoTemplate(factory, converter); template.setWriteConcernResolver(mongoAction -> { log.info("Action {} called on collection {} for document {} with WriteConcern.MAJORITY", mongoAction.getMongoActionOperation(), mongoAction.getCollectionName(), mongoAction.getCollectionName()); return WriteConcern.MAJORITY; }); template.setWriteResultChecking(WriteResultChecking.EXCEPTION);
If you'd like us to spend some time investigating, please take the time to provide a complete minimal sample (something that we can unzip or git clone, build, and deploy) that reproduces the problem.
Also please make use of GH formatting syntax when pasting code.
hey, not much I can export to provide you as sample as I am on proprietary system. if you have any particular code ask then I can sample that.
Looking at the already provided code, the setup (MappingContext
,...) seems to be done manually. This may involve configuration that leads to the error. Without a sample there won't be much we can do, since the defaults work as expected within the boundaries of our tests.
Mapping context is manual because I want to read collection name from properties and cannot hardcode in my pojo. If there is any other way to do that, except MappingContext then do let me know, will try out the change. I think if you use my MappingContext change as above then you will be able to reproduce the issue.
Closing this one. If you feel the issue is still valid pleas reopen and provide a complete minimal sample (something that we can unzip or git clone, build, and deploy) that reproduces the problem.