Spring没有正确地将对象保存到MongoDB

时间:2014-08-11 07:34:05

标签: java spring mongodb spring-boot

以下问题已与此问题分开: ArrayIndexOutOfBoundsException while Spring save data to MongoDB

将Object保存到MongoDB时遇到问题。我注意到这个问题可能是由太复杂的对象引起的。我有以下类层次结构: enter image description here

ClassAClassBClassC的超类。 ClassD包含地图地图。 ClassC包含ClassB

我调用的代码如下:

ClassC c = new ClassC()
c.setName("NAME");
mongoOperation.save(c, "Mongo"); // MongoOperations object

问题是Mongo不保存对象的数据。它仅保存_id_class

实际数据

{
    "_id" : ObjectId("53e86cd9c506f66eafaa03cb"),
    "_class" : "com.sample.ClassC"
}

预期数据

{
    "_id" : ObjectId("53e86cd9c506f66eafaa03cb"),
    "_class" : "com.sample.ClassC",
    "name" : "NAME"
}

有趣的是,当我在ClassD中注释掉地图字段时,一切正常。

是否可能是由于我尝试序列化过于复杂的对象造成的?


修改

当我从bObject移除ClassC时,它也可以正常工作。


编辑2

所有类都是带有setter和getter的简单bean。

e.g。

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

下面的完整示例,它具有与上图相同的类层次结构。

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;
    }

}

2 个答案:

答案 0 :(得分:3)

以下代码似乎有效:

@EnableAutoConfiguration
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 {
        Customer customer = new Customer("myself");
        ClassB classB = new ClassB();
        TreeMap<String, TreeMap<String, String>> map =  new TreeMap<String, TreeMap<String, String>>();
        TreeMap<String, String> innermap = new TreeMap<String, String>();
        innermap.put("iam", "cool");
        map.put("innermap", innermap);
        TreeMap<String, String> innermap2 = new TreeMap<String, String>();
        innermap2.put("youare", "yellow");
        map.put("innermap2", innermap2);
        classB.setMap(map);
        customer.setClassB(classB);
        try {
            mongoTemplate.save(customer);
        } catch (Exception e) {
            e.printStackTrace();
        }
        mongoTemplate.save(customer);
        System.out.println(mongoTemplate.findAll(Customer.class));;
    }
}


public class ClassB {

    private TreeMap<String, TreeMap<String, String>> map = new TreeMap<String, TreeMap<String, String>>();

    public TreeMap<String, TreeMap<String, String>> getMap() {
        return map;
    }

    public void setMap(TreeMap<String, TreeMap<String, String>> map) {
        this.map = map;
    }
}


@Document(collection ="customer")
public class Customer {

    @Id
    private String id;

    private String name;

    private ClassB classB;

    public Customer() {
    }

    public Customer(String name) {
        this.name = name;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public ClassB getClassB() {
        return classB;
    }

    public void setClassB(ClassB classB) {
        this.classB = classB;
    }

    @Override
    public String toString() {
        return "Customer [id=" + id + ", name=" + name + ", classB=" + classB
                + "]";
    }

}

the ArrayIndexOutOfBoundsException-issue仍然存在。

答案 1 :(得分:3)

我猜你的spring-data-mongodb.jar的特定版本中的MongoConverter工作不正常。 Spring必须将ClassC实例转换为DBObject格式,然后调用DBCollection.save将数据保存到数据库中。您可以检查方法DBObject"com.mongodb.DBCollection.save"参数的内容是否包含您期望的正确数据。

我复制了ClassC完整的结构和测试,它很好,无法重现您上面描述的内容。我使用spring-data-mongdb-1.2.3-RELEASE.jar。您采用的版本是什么?