I am using Spring Data Commons – 1.4.0.RC1 and Spring Data MongoDB – 1.1.0.RC1, having a problem with updating a list of embedded documents(of the same class hierarchy) using mongoTemplate.findAndModify(), which does not set the “_class” value as mongoTemplate.save() does, causing problems when reading data from mongodb as the application does not know which concrete class to instantiate for each embedded doc in that list.
For instance, I have a Student class, and a student has enrolled on subjects such as Maths, Physics, Computer Science, etc. This is not the domain I am working with, but should be enough to demonstrate the problem. So I will have a Student class mapped to a “student” collection and a list of subjects as embedded documents and all subjects extends the Subject super class, as below :
@Document
public class Student {
@Id
private String identifier;
private List<Subject> subjcts;
}
public abstract class Subject {
// common properties and methods;
}
public class Maths extends Subject {}
public class Physics extends Subject{}
To create a Student and save to database, I do
Student student = new Student();
student.setIdentifier(UUID.random().toString);
student.addSubject(new Maths());
student.addSubject(new Physics());
mongoTemplate.insert(student); or mongoTemplate.save(student);
The above will create a student with subjects maths and physics as embedded documents with an extra field “_class” specifying the concrete class so that it can be mapped to the correct class while fetching from the db.
However, if now i want to update the list of subjects, it is intuitive to do something like mongoTemplate.findAndModify(the query, new Update().set("subject", newSubjects), Student.class);
but findAndModify does not seem to go through the type mapper which sets the “_class” for the subjects, hence causes problems in reading back from db.
Is it a bug in Spring Data ? How do I work around it?
Any pointer will be appreciated.
Thank you.
This is a known issue of SpringData MongoDB (https://jira.springsource.org/browse/DATAMONGO-392).
For now the only workaround I found is to create a specific converter for the inner-document class.