如何将最终字段添加到现有的spring-data-mongodb文档集合中?

我有一个使用spring-data-mongodb版本1.0.2.RELEASE的现有文档集合。

 @Document public class Snapshot { @Id private final long id; private final String description; private final boolean active; @PersistenceConstructor public Snapshot(long id, String description, boolean active) { this.id = id; this.description = description; this.active = active; } } 

我正在尝试添加一个新属性private final boolean billable; 。 由于属性是final因此需要在构造函数中设置它们。 如果我将新属性添加到构造函数,则应用程序将无法再读取现有文档。

 org.springframework.data.mapping.model.MappingInstantiationException: Could not instantiate bean class [com.some.package.Snapshot]: Illegal arguments for constructor; 

据我所知,你不能将多个构造函数声明为@PersistenceContstructor所以除非我手动更新现有文档以包含可billable字段,否则我无法将final属性添加到此现有集合中。

以前有没有人找到解决方案?

我发现只使用@PersistenceContstructor注释就不可能将新的private final字段添加到现有集合中。 相反,我需要添加一个org.springframework.core.convert.converter.Converter实现来为我处理逻辑。

这是我的转换器最终看起来像:

 @ReadingConverter public class SnapshotReadingConverter implements Converter { @Override public Snapshot convert(DBObject source) { long id = (Long) source.get("_id"); String description = (String) source.get("description"); boolean active = (Boolean) source.get("active"); boolean billable = false; if (source.get("billable") != null) { billable = (Boolean) source.get("billable"); } return new Snapshot(id, description, active, billable); } } 

我希望这可以在将来帮助别人。