spring-data-mongodb icon indicating copy to clipboard operation
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

Open rajeshatuce opened this issue 2 years ago • 5 comments

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 <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-mongodb</artifactId> 2.6.6

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 ?

rajeshatuce avatar May 24 '22 10:05 rajeshatuce

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);

rajeshatuce avatar May 24 '22 10:05 rajeshatuce

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.

christophstrobl avatar May 24 '22 11:05 christophstrobl

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.

rajeshatuce avatar May 24 '22 11:05 rajeshatuce

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.

christophstrobl avatar May 24 '22 11:05 christophstrobl

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.

rajeshatuce avatar May 24 '22 12:05 rajeshatuce

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.

christophstrobl avatar Dec 22 '23 12:12 christophstrobl