Results 1 to 6 of 6

Thread: Spring data MongoDb: MappingMongoConverter remove _class

  1. #1
    Join Date
    Jun 2010
    Posts
    2

    Default Spring data MongoDb: MappingMongoConverter remove _class

    The default MappingMongoConverter adds a custom type key ("_class") to each object in the database. So, if I create a Person:

    Code:
        
        package my.dto;
        public class Person {
            String name;
            public Person(String name) {
                this.name = name; 
            }
        }
    and save it to the db:

    Code:
        MongoOperations ops = new MongoTemplate(new Mongo(), "users");
        ops.insert(new Person("Joe"));
    the resulting object in the mongo will be:

    Code:
        { "_id" : ObjectId("4e2ca049744e664eba9d1e11"), "_class" : "my.dto.Person", "name" : "Joe" }

    Questions:

    1. What are the implications of moving the Person class into a different namespace?

    2. Is it possible not to pollute the object with the "_class" key; without writing a unique converter just for the Person class?

    Also posted on StackOverflow.
    Last edited by georgecalm; Jul 24th, 2011 at 07:18 PM. Reason: Adding link to SO question.

  2. #2
    Join Date
    Mar 2011
    Posts
    5

    Default Did you ever find a workaround for this?

    So what is the story with this? Is there no way to prevent the "_class" field from getting stored in MongoDB?

  3. #3
    Join Date
    Apr 2006
    Location
    Dresden, Germany
    Posts
    492

    Default

    You can generally prevent the _class attribute being written by reconfiguring the DefaultMongoTypeMapper instance on MappingMongoConverter and setting the key to null:

    Code:
    MappingMongoConverter converter = new MappingMongoConverter();
    converter.setTypeMapper(new DefaultMongoTypeMapper(null));
    The type information inside the document actually serves as type hint in case you're querying the the collection for superclasses or interfaces. If the type information in the document is not more concrete than the one actually requested is not considered. To be more safe agains potential refactorings consider using ConfigurableTypeInformationMapper with DefaultMongoTypeMapper like:

    Code:
    Map<? extends Class<?>, String> typeMap = … // Build map of types to type aliases
    MongoTypeMapper mapper = new DefaultMongoTypeMapper("_class", new ConfigurableTypeInformationMapper(typeMap));
    MappingMongoConverter converter = new MappingMongoConverter();
    converter.setTypeMapper(mapper);
    Note that by default we will setup a MongoTypeMapper inspecting the MappingContext for type aliases that can be annotated with @TypeAlias to the entity classes. So if you do so, we will write the alias instead of the class name which allows moving the class pretty much everywhere.

  4. #4
    Join Date
    Mar 2011
    Posts
    5

    Default Thanks so much for your reply.

    I will give your suggestions a try!

  5. #5
    Join Date
    Mar 2012
    Posts
    9

    Default

    Sorry for waking up this old thread.
    MappingMongoConverter doesn't seem to have a no-arg constructor; am I missing something?

    If there's a new answer to the question (how to remove _class), can someone please reply?

  6. #6
    Join Date
    Mar 2012
    Posts
    9

    Default

    Created it as in the MongoTemplate source:

    MappingMongoConverter converter = new MappingMongoConverter(factory, new MongoMappingContext());\

    it works.

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
  •