Spring Data MongoDB
  1. Spring Data MongoDB
  2. DATAMONGO-44

Id property is overridden by Spring Data's own Id property

    Details

    • Type: Bug Bug
    • Status: Closed
    • Priority: Major Major
    • Resolution: Won't Fix
    • Affects Version/s: None
    • Fix Version/s: 1.0 M2
    • Component/s: Core
    • Labels:
      None
    • Environment:
      Any OS, MongoDB 1.6.5, Spring Data for MongoDB 1.0.0.M1

      Description

      When creating a simple POJO with an id property and persisting this object to MongoDB, the id property is overridden by Spring Data. It merges the "id" with "_id".

      For example:

      { "_id" : "69133244-fcc0-42b2-aa59-308f00c75860", "firstName" : "John", "money" : 1000, "lastName" : "Smith" } { "_id" : "45d236ff-6a38-4767-a973-6f3d70ff2448", "firstName" : "Jane", "money" : 2000, "lastName" : "Adams" } { "_id" : "9dcba269-9ed5-45c6-887e-a2de8fec334d", "firstName" : "Jeff", "money" : 3000, "lastName" : "Mayer" }

      The native MongoDB preserves both the "id" and "_id", while Spring Data combines both as "_id". As a workaround, I have to rename my POJO's id property as "pid" (any other name will work).

      For example:

      { "_id" : ObjectId("4d5e7b3bfdece02281f253a4"), "id" : "f1e6b8b2-9d68-495c-9d6e-b81cc35cf9bc", "firstName" : "John", "lastName" : "Smith", "money" : 1000 } { "_id" : ObjectId("4d5e7b3bfdece02282f253a4"), "id" : "39f6ebfb-10cf-4702-a5b8-8bc2489cebcc", "firstName" : "Jane", "lastName" : "Adams", "money" : 2000 }

      { "_id" : ObjectId("4d5e7b3bfdece02283f253a4"), "id" : "f4932004-c320-47e4-b4b3-7d07f3aaabb9", "firstName" : "Jeff", "lastName" : "Mayer", "money" : 3000

      See the following tutorial that shows this "pid" workaround: http://krams915.blogspot.com/2011/02/spring-data-mongodb-tutorial.html

      See the following tutorial that uses native MongoDB which correctly preserves both "id" and "_id": http://krams915.blogspot.com/2011/01/spring-mvc-3-using-document-oriented.html

        Issue Links

          Activity

          Hide
          Thomas Risberg added a comment -

          This is more or less by design. Any id property/field is mapped to the _id field in the document that is stored in MongoDB. When using the mapping support anything annotated with @Id will be stored in the _id field. We didn't see any good reason to maintain two separate id fields and it seemed more natural to a Java developer to have a field named id or annotated with @Id rather than being forced to use _id in your Java classes. You can always use custom readers/writers when using the MongoTemplate if the provided converters aren't flexible enough.

          -Thomas

          Show
          Thomas Risberg added a comment - This is more or less by design. Any id property/field is mapped to the _id field in the document that is stored in MongoDB. When using the mapping support anything annotated with @Id will be stored in the _id field. We didn't see any good reason to maintain two separate id fields and it seemed more natural to a Java developer to have a field named id or annotated with @Id rather than being forced to use _id in your Java classes. You can always use custom readers/writers when using the MongoTemplate if the provided converters aren't flexible enough. -Thomas
          Hide
          Mark Serrano added a comment -

          Thomas, thanks for the response and time given to this issue. I agree with your points. When you wrote "This is more or less by design" I wish the docs would be more transparent when it comes to such design decision. Without relying on Spring Data MongoDB the application works fine regardless if you have the "_id" and a different field acting as the id. In any case, I'm good with the M2 release.

          Show
          Mark Serrano added a comment - Thomas, thanks for the response and time given to this issue. I agree with your points. When you wrote "This is more or less by design" I wish the docs would be more transparent when it comes to such design decision. Without relying on Spring Data MongoDB the application works fine regardless if you have the "_id" and a different field acting as the id. In any case, I'm good with the M2 release.

            People

            • Assignee:
              Thomas Risberg
              Reporter:
              Mark Serrano
            • Votes:
              0 Vote for this issue
              Watchers:
              0 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved: