问题
I am using spring Mongo Template to persist data to MongoDb. I have a custom object which has a list.
Public Class CustomObject implements Serializable{
private CustomType1 header;
private List<CustomType2> Values;
}
I created a wrapper class in order to wrap my custom object and the wrapper class looks like this
public Class Wrapper {
private String id;
private Object object;
}
I am calling the save method like below
Wrapper wrapper = new wrapper(key, value);
mongoTemplate.save(wrapper, collectionName);
I am able to save the CustomObject to MongoDB but when I check the the document in the mongodb it is only showing the "header", the "values" field is getting ignored. It is not giving any error.
Can anyone help me with this?
回答1:
i think this is the flow you want..
@Document
Public Class CustomObject{
private CustomType1 header;
private List<CustomType2> values;
}
@Document
public Class Wrapper {
@Id
private String key;
private CustomObject value;
}
...
...
CustomObject customObject = new CustomObject(header,values);
Wrapper wrapper = new Wrapper(key, customObject);
mongoTemplate.save(wrapper)
;
来源:https://stackoverflow.com/questions/51942478/spring-mongo-template-not-saving-the-list-of-custom-objects-to-mongodb