Announcement Announcement Module
No announcement yet.
How can I convert correctly. Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • How can I convert correctly.

    I use Spring Data - MongoDB 1.0.0.M3 and Spring 3.1.0.M2.

    I wrote below class that has a

    public class Parent {
    	private String id;
    	private String message;
    	private Object child;
            (getter and setter ...)
    public class Child {
    	private String id;
    	private String name;
    I tryed to insert into DB by using MongoTemplate.insert().
    Then I checked MongoDB by using MongoDB shell client as below

    > db.parent.find()

    I expected to get a value as follows.
    { "_id" : ObjectId("4e51f21cda549a6920d927d1"), "_class" : "org.sample.mongo.Parent", "child" : { "name" : "child name"}, "message" : "This is message" }
    But I got this.
    { "_id" : ObjectId("4e51f21cda549a6920d927d1"), "_class" : "org.sample.mongo.Parent", "child" : {"_class" : "org.sample.mongo.Child"}, "message" : "This is message" }
    How can I get collect result of child field ?(not "_class" but "name"!)

  • #2
    I am having this same problem and I find it amazing that Spring Data doesn't support this basic requirement.

    To summarize what egiost69 is saying:

    Spring does not support polymorphism upon save. If you have a field which has a superclass as it's type, the field will not be saved properly.
    For example:
    public class Animal {}
    public class Lion extends Animal {}
    public class Zoo {
      // does not save properties of subclass Lion if added to this list
      List<Animal> animals;
    In the above example, if you added a Lion to the list of animals in the Zoo, it will not save the fields specific to the Lion, however it will correctly save the classname of the Lion.

    The only workaround for this is to save the list as a DBRef.