for example, if we want mongodb only store the id of some part of entities, the converter seems like:
Code:
public final class DBObjectToEntityFactory implements ConverterFactory<DBObject, BaseEntity> {
public <T extends BaseEntity> Converter<DBObject, T> getConverter(Class<T> targetType) {
return new DBObjectToManagedEntityConverter<T>();
}
private final class DBObjectToManagedEntityConverter<T extends BaseEntity> implements Converter<DBObject, T> {
@Autowired CrudRepository<T, Long> repo;
public T convert(DBObject source){
return (T) repo.findOne((Long) source.get("id"));
}
}
}
public class EntityToDBObject implements Converter<BaseEntity, DBObject> {
public DBObject convert(BaseEntity source) {
DBObject dbo = new BasicDBObject();
dbo.put("id", source.getId());
return dbo;
}
}