I have an existing document collection using spring-data-mongodb
version 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;
}
}
I'm trying to add a new property private final boolean billable;
. Since the properties are final
they need to be set in the constructor. If I add the new property to the constructor, then the application can no longer read the existing docs.
org.springframework.data.mapping.model.MappingInstantiationException: Could not instantiate bean class [com.some.package.Snapshot]: Illegal arguments for constructor;
As far as I can tell, you cannot have multiple constructors declared as @PersistenceContstructor
so unless I manually update the existing documents to contain the billable
field, I have no way to add a final
property to this existing collection.
Has anyone found a solution to this before?
I found that it is not possible to add a new
private final
field to an existing collection using only the@PersistenceContstructor
annotation. Instead I needed to add anorg.springframework.core.convert.converter.Converter
implementation to handle the logic for me.Here's what my converter ended up looking like:
I hope this can help someone else in the future.