I found a "solution". It seems to be, that the mapping is created first when an object has been converted into a DBObject. The solution is to convert one object of each class which can be stored in the collection one time. After this the converter seems to know about the mapping.
Is this a bug? Do I do something wrong? I appended my configuration class:
Code:
package com.rebuy.service.cm.configuration;
import java.net.UnknownHostException;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.data.authentication.UserCredentials;
import org.springframework.data.mongodb.config.AbstractMongoConfiguration;
import org.springframework.data.mongodb.core.convert.MappingMongoConverter;
import org.springframework.data.mongodb.repository.config.EnableMongoRepositories;
import com.mongodb.Mongo;
import com.mongodb.WriteConcern;
import com.rebuy.service.cm.persistence.document.widget.ImageWidget;
import com.rebuy.service.cm.persistence.document.widget.TextWidget;
@Configuration
@EnableMongoRepositories
@PropertySource("classpath:/${env}/nosql.properties")
public class MongoDbConfiguration extends AbstractMongoConfiguration
{
@Value("${mongo.host}")
private String host;
@Value("${mongo.port}")
private Integer port;
@Value("${mongo.dbname}")
private String dbname;
@Value("${mongo.secure}")
private Boolean secure;
@Value("${mongo.username}")
private String username;
@Value("${mongo.password}")
private String password;
@Value("${mongo.typeKey}")
private String typeKey;
@Override
@Bean
public Mongo mongo() throws UnknownHostException
{
Mongo mongo = new Mongo(host, port);
mongo.setWriteConcern(WriteConcern.SAFE);
return mongo;
}
@Override
protected String getDatabaseName()
{
return dbname;
}
@Override
protected UserCredentials getUserCredentials() {
if (secure) {
return new UserCredentials(username, password);
}
return null;
}
@Bean
@Override
public MappingMongoConverter mappingMongoConverter() throws Exception {
MappingMongoConverter converter = super.mappingMongoConverter();
converter.convertToMongoType(new TextWidget());
//converter.convertToMongoType(new ImageWidget());
registerCustomConverters(converter);
return converter;
}
private void registerCustomConverters(MappingMongoConverter converter)
{
//List<Converter<Object, Object>> conversions= new ArrayList<Converter<Object, Object>>();
//converter.setCustomConversions(new CustomConversions(conversions));
}
}