Spring Mongo Template not saving the list of custom objects to MongoDb

主宰稳场 提交于 2019-12-25 03:11:23

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!