Results 1 to 2 of 2

Thread: How can I convert correctly.

  1. #1
    Join Date
    Aug 2011
    Posts
    3

    Default 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

    Code:
    public class Parent {
    	private String id;
    	private String message;
    	private Object child;
    
            (getter and setter ...)
    }
    Code:
    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.
    Code:
    { "_id" : ObjectId("4e51f21cda549a6920d927d1"), "_class" : "org.sample.mongo.Parent", "child" : { "name" : "child name"}, "message" : "This is message" }
    But I got this.
    Code:
    { "_id" : ObjectId("4e51f21cda549a6920d927d1"), "_class" : "org.sample.mongo.Parent", "child" : {"_class" : "org.sample.mongo.Child"}, "message" : "This is message" }
    Why?
    How can I get collect result of child field ?(not "_class" but "name"!)

  2. #2
    Join Date
    Sep 2011
    Posts
    1

    Default

    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:
    Code:
    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.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •