Spring无法将对象正确保存到MongoDB [英] Spring doesn't save object to MongoDB correctly
问题描述
以下问题已与该问题分开: ArrayIndexOutOfBoundsException,而Spring将数据保存到MongoDB
我在将对象保存到MongoDB时遇到问题.我注意到问题可能是由太复杂的对象引起的.我有以下类层次结构:
I have problem with saving Object to MongoDB. I've noticed that problem might be caused by too complex object. I have following class hierarchy:
ClassA
是ClassB
和ClassC
的超类. ClassD
包含地图的地图. ClassC
包含ClassB
.
ClassA
is superclass for ClassB
and ClassC
. ClassD
contains map of maps. ClassC
contains ClassB
.
我调用的代码如下:
ClassC c = new ClassC()
c.setName("NAME");
mongoOperation.save(c, "Mongo"); // MongoOperations object
问题在于Mongo不会保存对象的数据.它仅保存_id
和_class
.
The problem is that Mongo doesn't save object's data. It saves only _id
and _class
.
实际数据
{
"_id" : ObjectId("53e86cd9c506f66eafaa03cb"),
"_class" : "com.sample.ClassC"
}
预期数据
{
"_id" : ObjectId("53e86cd9c506f66eafaa03cb"),
"_class" : "com.sample.ClassC",
"name" : "NAME"
}
有趣的是,当我在ClassD
中注释掉地图字段时,一切正常.
Funny thing is that when I comment out map field in ClassD
everything works fine.
是否可能是因为我尝试序列化的对象太复杂?
编辑
当我从ClassC
中删除bObject
时,它也可以正常工作.
When I remove bObject
from ClassC
it also works fine.
编辑2
所有类都是带有setter和getter的简单bean.
All classes are simple beans with setters and getters.
例如
public class ClassD{
private TreeMap<String, TreeMap<String,String>> map;
public TreeMap<String, TreeMap<String, String>> getMap() {
return map;
}
public void setMap(TreeMap<String, TreeMap<String, String>> map) {
this.map = map;
}
}
编辑3
下面的完整示例,它具有与上图相同的类层次结构.
Full example below, it has same class hierarchy as picture above.
public class Application implements CommandLineRunner {
@Autowired
private MongoTemplate mongoTemplate;
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
@Override
public void run(String... args) throws Exception {
ClassC cObject = new ClassC();
cObject.setName("Jon");
try {
mongoTemplate.save(cObject);
}catch(Exception e){
e.printStackTrace();
}
mongoTemplate.save(cObject);
}
}
class ClassA{
private String name;
private ClassD dObject;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public ClassD getdObject() {
return dObject;
}
public void setdObject(ClassD dObject) {
this.dObject = dObject;
}
}
class ClassB extends ClassA {
}
class ClassC extends ClassA {
private ClassB b;
public ClassB getB() {
return b;
}
public void setB(ClassB b) {
this.b = b;
}
}
class ClassD {
private TreeMap<String, TreeMap<String, String>> map = new TreeMap<>();
public TreeMap<String, TreeMap<String, String>> getMap() {
return map;
}
public void setMap(TreeMap<String, TreeMap<String, String>> map) {
this.map = map;
}
}
推荐答案
我想您的spring-data-mongodb.jar特定版本中的MongoConverter
无法正常工作.
Spring必须将您的ClassC
实例转换为DBObject
格式,然后调用DBCollection.save
将数据保存到数据库中.您可以在方法"com.mongodb.DBCollection.save"
中检查DBObject
参数的内容,是否包含所需的正确数据.
I guess the MongoConverter
in specific version of your spring-data-mongodb.jar works incorrectly.
Spring must convert your ClassC
instance into DBObject
format, then call DBCollection.save
to save data into database. You can check the content of DBObject
parameter in method "com.mongodb.DBCollection.save"
whether it contains correct data as you expect.
我使用完整的结构和测试来复制您的ClassC
,这很好,不能复制您上面描述的内容.我使用spring-data-mongdb-1.2.3-RELEASE.jar.您采用什么版本?
I copy your ClassC
with complete structure and test, it's fine and cannot reproduce what you described above. I use spring-data-mongdb-1.2.3-RELEASE.jar. What's the version you adopt?
这篇关于Spring无法将对象正确保存到MongoDB的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!