6

I have been trying to register my own write custom converters to change the default ID value. But it never actually calls. Here is my Custom Converter

public class EventKeyConverter implements Converter<Event,DBObject> {

    @Override
    public DBObject convert(Event object) {
        DBObject dbObject = DBObjectTransformer.toDBObject(object);
        dbObject.put("_id", KeyGenerator.getRandomKey());
        return dbObject;
    }

}

here is the place that I did register customer converter

@Override
@Bean
public CustomConversions customConversions() {
    List<Converter<?, ?>> converters = new ArrayList<Converter<?, ?>>();
    converters.add(new EventKeyConverter());
    return new CustomConversions(converters);
}

@Override
@Bean
public MappingMongoConverter mappingMongoConverter() throws Exception {
    MappingMongoConverter converter = new MappingMongoConverter(
            eventsMongoDbFactory(), mongoMappingContext());
    converter.setCustomConversions(customConversions());
    return converter;
}

@Bean
public MongoTemplate eventsMongoTemplate() throws Exception {
    final MongoTemplate template = new MongoTemplate(
            eventsMongoDbFactory(), mappingMongoConverter());
    template.setWriteResultChecking(WriteResultChecking.EXCEPTION);

    return template;
}

When I'm saving some objects this converter never gets called.


Edit 1 : I need to change the default object Id into some custom Id (UUID + random key) in all repositories. That why I tried to use mongo converter.

Edit 2 : Just found the issue. Change @Configuration to @Component in class that contains customConversion() and it works fine. But still wondering what is happened here ?

Brad Mace
  • 27,194
  • 17
  • 102
  • 148
Rajith Delantha
  • 713
  • 11
  • 21
  • Could you please show how you actually save the objects? I mean, why would you need to convert a POJO to actual BSON to save it to DB? – user Feb 18 '13 at 12:56
  • I need to change the default object Id into some custom Id (UUID + random key) in all repositories. That why I tried to use mongo converter. – Rajith Delantha Feb 19 '13 at 05:20
  • Rajith, in mongodb you are not allowed to update the _id fields. If you would like to add new objects with a custom _id, you should provide the needed field with the @Id annotation and when you insert an object manually set the _id. – user Feb 19 '13 at 08:44
  • just want to do same thing like this .. http://static.springsource.org/spring-data/data-mongo/docs/1.0.0.M5/reference/html/#d0e2670 – Rajith Delantha Feb 19 '13 at 09:06
  • Works for me - I put the @Component tag and now Spring uses my convertors – Ido Cohn Jul 28 '13 at 14:34

1 Answers1

2

@Configuration defines a Spring context fragment that contains methods that if annotated with @Bean return new beans and put them in the context.

@Component is a way of saying "this Pojo is a Spring bean". You then need a @ComponentScan annotation or XML equivalent to scan packages for @Component annotated beans.

So in your case, you created the converter fine, but it wasn't registered as a Spring bean until you added the @Component, so it didn't work initially.

Brad Mace
  • 27,194
  • 17
  • 102
  • 148
PaulNUK
  • 4,774
  • 2
  • 30
  • 58